2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.Plan
|
2022-04-08 09:48:37 +03:00
|
|
|
( SourceConfig (..),
|
|
|
|
mkPlan,
|
2022-06-07 04:34:37 +03:00
|
|
|
renderQuery,
|
2022-04-08 09:48:37 +03:00
|
|
|
queryHasRelations,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-06-24 09:58:25 +03:00
|
|
|
import Control.Monad.Trans.Writer.CPS qualified as CPS
|
2022-04-08 09:48:37 +03:00
|
|
|
import Data.Aeson qualified as J
|
2022-04-14 05:06:07 +03:00
|
|
|
import Data.ByteString.Lazy qualified as BL
|
2022-04-08 09:48:37 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
|
|
|
import Data.List.NonEmpty qualified as NE
|
2022-06-24 09:58:25 +03:00
|
|
|
import Data.Semigroup (Min (..))
|
2022-04-14 05:06:07 +03:00
|
|
|
import Data.Text.Encoding qualified as TE
|
2022-06-02 05:06:45 +03:00
|
|
|
import Data.Text.Extended ((<>>))
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.Adapter.Types
|
2022-06-02 05:06:45 +03:00
|
|
|
import Hasura.Backends.DataConnector.IR.Column qualified as IR.C
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.IR.Export qualified as IR
|
|
|
|
import Hasura.Backends.DataConnector.IR.Expression qualified as IR.E
|
|
|
|
import Hasura.Backends.DataConnector.IR.OrderBy qualified as IR.O
|
|
|
|
import Hasura.Backends.DataConnector.IR.Query qualified as IR.Q
|
2022-06-24 09:58:25 +03:00
|
|
|
import Hasura.Backends.DataConnector.IR.Relationships qualified as IR.R
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.IR.Scalar.Value qualified as IR.S
|
2022-06-24 09:58:25 +03:00
|
|
|
import Hasura.Backends.DataConnector.IR.Table qualified as IR.T
|
2022-04-08 09:48:37 +03:00
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.BoolExp
|
|
|
|
import Hasura.RQL.IR.OrderBy
|
|
|
|
import Hasura.RQL.IR.Select
|
2022-05-31 01:07:02 +03:00
|
|
|
import Hasura.RQL.IR.Value
|
2022-04-08 09:48:37 +03:00
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common
|
2022-06-24 09:58:25 +03:00
|
|
|
import Hasura.RQL.Types.Relationships.Local (RelInfo (..))
|
2022-04-08 09:48:37 +03:00
|
|
|
import Hasura.SQL.Backend
|
|
|
|
import Hasura.Session
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Error type for the postProcessor continuation. Failure can occur if the Agent
|
|
|
|
-- returns bad data.
|
|
|
|
data ResponseError
|
|
|
|
= RequiredFieldMissing
|
|
|
|
| UnexpectedFields
|
|
|
|
| ExpectedObject
|
|
|
|
| ExpectedArray
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
-- | Extract the 'IR.Q' from a 'Plan' and render it as 'Text'.
|
2022-04-14 05:06:07 +03:00
|
|
|
--
|
|
|
|
-- NOTE: This is for logging and debug purposes only.
|
2022-06-24 09:58:25 +03:00
|
|
|
renderQuery :: IR.Q.QueryRequest -> Text
|
2022-06-07 04:34:37 +03:00
|
|
|
renderQuery =
|
2022-06-24 09:58:25 +03:00
|
|
|
TE.decodeUtf8 . BL.toStrict . J.encode . IR.queryRequestToAPI
|
2022-04-14 05:06:07 +03:00
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
-- | Map a 'QueryDB 'DataConnector' term into a 'Plan'
|
2022-04-08 09:48:37 +03:00
|
|
|
mkPlan ::
|
|
|
|
forall m.
|
|
|
|
MonadError QErr m =>
|
|
|
|
SessionVariables ->
|
|
|
|
SourceConfig ->
|
2022-05-02 08:03:12 +03:00
|
|
|
QueryDB 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-06-24 09:58:25 +03:00
|
|
|
m IR.Q.QueryRequest
|
2022-06-07 04:34:37 +03:00
|
|
|
mkPlan session (SourceConfig {}) ir = translateQueryDB ir
|
2022-04-08 09:48:37 +03:00
|
|
|
where
|
|
|
|
translateQueryDB ::
|
2022-05-02 08:03:12 +03:00
|
|
|
QueryDB 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-06-24 09:58:25 +03:00
|
|
|
m IR.Q.QueryRequest
|
2022-04-08 09:48:37 +03:00
|
|
|
translateQueryDB =
|
2022-06-02 05:06:45 +03:00
|
|
|
\case
|
2022-06-24 09:58:25 +03:00
|
|
|
QDBMultipleRows s -> translateAnnSelectToQueryRequest s
|
|
|
|
QDBSingleRow s -> translateAnnSelectToQueryRequest s
|
2022-04-08 09:48:37 +03:00
|
|
|
QDBAggregation {} -> throw400 NotSupported "QDBAggregation: not supported"
|
|
|
|
|
2022-06-24 09:58:25 +03:00
|
|
|
translateAnnSelectToQueryRequest ::
|
2022-06-02 05:06:45 +03:00
|
|
|
AnnSimpleSelectG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-06-24 09:58:25 +03:00
|
|
|
m IR.Q.QueryRequest
|
|
|
|
translateAnnSelectToQueryRequest selectG = do
|
|
|
|
tableName <- extractTableName selectG
|
|
|
|
(query, tableRelationships) <- CPS.runWriterT (translateAnnSelect tableName selectG)
|
|
|
|
pure $
|
|
|
|
IR.Q.QueryRequest
|
|
|
|
{ _qrTable = tableName,
|
|
|
|
_qrTableRelationships = tableRelationships,
|
|
|
|
_qrQuery = query
|
|
|
|
}
|
|
|
|
|
|
|
|
extractTableName :: AnnSimpleSelectG 'DataConnector Void v -> m IR.T.Name
|
|
|
|
extractTableName selectG =
|
|
|
|
case _asnFrom selectG of
|
2022-04-08 09:48:37 +03:00
|
|
|
FromTable tn -> pure tn
|
2022-06-02 05:06:45 +03:00
|
|
|
FromIdentifier _ -> throw400 NotSupported "AnnSelectG: FromIdentifier not supported"
|
|
|
|
FromFunction {} -> throw400 NotSupported "AnnSelectG: FromFunction not supported"
|
2022-06-24 09:58:25 +03:00
|
|
|
|
|
|
|
recordTableRelationship :: IR.T.Name -> IR.R.RelationshipName -> IR.R.Relationship -> CPS.WriterT IR.R.TableRelationships m ()
|
|
|
|
recordTableRelationship sourceTableName relationshipName relationship =
|
|
|
|
CPS.tell $ HashMap.singleton sourceTableName (HashMap.singleton relationshipName relationship)
|
|
|
|
|
|
|
|
translateAnnSelect ::
|
|
|
|
IR.T.Name ->
|
|
|
|
AnnSimpleSelectG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
|
|
|
CPS.WriterT IR.R.TableRelationships m IR.Q.Query
|
|
|
|
translateAnnSelect tableName selectG = do
|
|
|
|
fields <- translateFields tableName (_asnFields selectG)
|
2022-04-08 09:48:37 +03:00
|
|
|
let whereClauseWithPermissions =
|
2022-06-02 05:06:45 +03:00
|
|
|
case _saWhere (_asnArgs selectG) of
|
|
|
|
Just expr -> BoolAnd [expr, _tpFilter (_asnPerm selectG)]
|
|
|
|
Nothing -> _tpFilter (_asnPerm selectG)
|
2022-06-24 09:58:25 +03:00
|
|
|
whereClause <- translateBoolExp [] tableName whereClauseWithPermissions
|
|
|
|
orderBy <- lift $ translateOrderBy (_saOrderBy $ _asnArgs selectG)
|
2022-04-08 09:48:37 +03:00
|
|
|
pure
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Query
|
2022-06-24 09:58:25 +03:00
|
|
|
{ _qFields = fields,
|
|
|
|
_qLimit =
|
2022-04-08 09:48:37 +03:00
|
|
|
fmap getMin $
|
|
|
|
foldMap
|
|
|
|
(fmap Min)
|
2022-06-02 05:06:45 +03:00
|
|
|
[ _saLimit (_asnArgs selectG),
|
|
|
|
_tpLimit (_asnPerm selectG)
|
2022-04-08 09:48:37 +03:00
|
|
|
],
|
2022-06-24 09:58:25 +03:00
|
|
|
_qOffset = fmap fromIntegral (_saOffset (_asnArgs selectG)),
|
|
|
|
_qWhere = Just whereClause,
|
|
|
|
_qOrderBy = orderBy
|
2022-04-08 09:48:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
translateOrderBy ::
|
2022-06-02 05:06:45 +03:00
|
|
|
Maybe (NE.NonEmpty (AnnotatedOrderByItemG 'DataConnector (UnpreparedValue 'DataConnector))) ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m [IR.O.OrderBy]
|
2022-04-08 09:48:37 +03:00
|
|
|
translateOrderBy = \case
|
|
|
|
Nothing -> pure []
|
|
|
|
Just orderBys ->
|
|
|
|
do
|
|
|
|
NE.toList
|
|
|
|
<$> for orderBys \OrderByItemG {..} -> case obiColumn of
|
|
|
|
AOCColumn (ColumnInfo {ciColumn = dynColumnName}) ->
|
|
|
|
pure
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.O.OrderBy
|
2022-04-08 09:48:37 +03:00
|
|
|
{ column = dynColumnName,
|
|
|
|
-- NOTE: Picking a default ordering.
|
2022-04-28 04:51:58 +03:00
|
|
|
ordering = fromMaybe IR.O.Ascending obiType
|
2022-04-08 09:48:37 +03:00
|
|
|
}
|
2022-06-02 05:06:45 +03:00
|
|
|
AOCObjectRelation {} ->
|
|
|
|
throw400 NotSupported "translateOrderBy: AOCObjectRelation unsupported in the Data Connector backend"
|
|
|
|
AOCArrayAggregation {} ->
|
|
|
|
throw400 NotSupported "translateOrderBy: AOCArrayAggregation unsupported in the Data Connector backend"
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateFields ::
|
2022-06-24 09:58:25 +03:00
|
|
|
IR.T.Name ->
|
2022-06-02 05:06:45 +03:00
|
|
|
AnnFieldsG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-06-24 09:58:25 +03:00
|
|
|
CPS.WriterT IR.R.TableRelationships m (HashMap Text IR.Q.Field)
|
|
|
|
translateFields sourceTableName fields = do
|
|
|
|
translatedFields <- traverse (traverse (translateField sourceTableName)) fields
|
2022-04-08 09:48:37 +03:00
|
|
|
pure $
|
|
|
|
HashMap.fromList $
|
2022-04-14 05:06:07 +03:00
|
|
|
mapMaybe
|
|
|
|
sequence
|
2022-06-02 05:06:45 +03:00
|
|
|
[(getFieldNameTxt f, field) | (f, field) <- translatedFields]
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateField ::
|
2022-06-24 09:58:25 +03:00
|
|
|
IR.T.Name ->
|
2022-06-02 05:06:45 +03:00
|
|
|
AnnFieldG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-06-24 09:58:25 +03:00
|
|
|
CPS.WriterT IR.R.TableRelationships m (Maybe IR.Q.Field)
|
|
|
|
translateField sourceTableName = \case
|
2022-06-02 05:06:45 +03:00
|
|
|
AFColumn colField ->
|
|
|
|
-- TODO: make sure certain fields in colField are not in use, since we don't
|
|
|
|
-- support them
|
2022-06-24 09:58:25 +03:00
|
|
|
pure . Just . IR.Q.ColumnField $ _acfColumn colField
|
2022-06-02 05:06:45 +03:00
|
|
|
AFObjectRelation objRel -> do
|
2022-06-24 09:58:25 +03:00
|
|
|
let targetTable = _aosTableFrom (_aarAnnSelect objRel)
|
|
|
|
let relationshipName = IR.R.mkRelationshipName $ _aarRelationshipName objRel
|
|
|
|
fields <- translateFields targetTable (_aosFields (_aarAnnSelect objRel))
|
|
|
|
whereClause <- translateBoolExp [] targetTable (_aosTableFilter (_aarAnnSelect objRel))
|
|
|
|
|
|
|
|
recordTableRelationship
|
|
|
|
sourceTableName
|
|
|
|
relationshipName
|
|
|
|
IR.R.Relationship
|
|
|
|
{ _rTargetTable = targetTable,
|
|
|
|
_rRelationshipType = IR.R.ObjectRelationship,
|
|
|
|
_rColumnMapping = _aarColumnMapping objRel
|
|
|
|
}
|
|
|
|
|
|
|
|
pure . Just . IR.Q.RelField $
|
|
|
|
IR.Q.RelationshipField
|
|
|
|
relationshipName
|
2022-06-02 05:06:45 +03:00
|
|
|
( IR.Q.Query
|
2022-06-24 09:58:25 +03:00
|
|
|
{ _qFields = fields,
|
|
|
|
_qWhere = Just whereClause,
|
|
|
|
_qLimit = Nothing,
|
|
|
|
_qOffset = Nothing,
|
|
|
|
_qOrderBy = []
|
2022-06-02 05:06:45 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
AFArrayRelation (ASSimple arrRel) -> do
|
2022-06-24 09:58:25 +03:00
|
|
|
targetTable <- lift $ extractTableName (_aarAnnSelect arrRel)
|
|
|
|
query <- translateAnnSelect targetTable (_aarAnnSelect arrRel)
|
|
|
|
let relationshipName = IR.R.mkRelationshipName $ _aarRelationshipName arrRel
|
|
|
|
|
|
|
|
recordTableRelationship
|
|
|
|
sourceTableName
|
|
|
|
relationshipName
|
|
|
|
IR.R.Relationship
|
|
|
|
{ _rTargetTable = targetTable,
|
|
|
|
_rRelationshipType = IR.R.ArrayRelationship,
|
|
|
|
_rColumnMapping = _aarColumnMapping arrRel
|
|
|
|
}
|
|
|
|
|
|
|
|
pure . Just . IR.Q.RelField $
|
|
|
|
IR.Q.RelationshipField
|
|
|
|
relationshipName
|
2022-06-02 05:06:45 +03:00
|
|
|
query
|
|
|
|
AFArrayRelation (ASAggregate _) ->
|
2022-06-24 09:58:25 +03:00
|
|
|
lift $ throw400 NotSupported "translateField: AFArrayRelation ASAggregate not supported"
|
2022-06-02 05:06:45 +03:00
|
|
|
AFExpression _literal ->
|
|
|
|
pure Nothing
|
|
|
|
|
|
|
|
prepareLiterals ::
|
2022-05-02 08:03:12 +03:00
|
|
|
UnpreparedValue 'DataConnector ->
|
2022-06-02 05:06:45 +03:00
|
|
|
m IR.S.Literal
|
|
|
|
prepareLiterals (UVLiteral literal) = pure $ literal
|
|
|
|
prepareLiterals (UVParameter _ e) = pure (IR.S.ValueLiteral (cvValue e))
|
|
|
|
prepareLiterals UVSession = throw400 NotSupported "prepareLiterals: UVSession"
|
|
|
|
prepareLiterals (UVSessionVar _ v) =
|
2022-04-08 09:48:37 +03:00
|
|
|
case getSessionVariableValue v session of
|
2022-06-02 05:06:45 +03:00
|
|
|
Nothing -> throw400 NotSupported ("prepareLiterals: session var not found: " <>> v)
|
|
|
|
Just s -> pure (IR.S.ValueLiteral (IR.S.String s))
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateBoolExp ::
|
2022-06-24 09:58:25 +03:00
|
|
|
[IR.R.RelationshipName] ->
|
|
|
|
IR.T.Name ->
|
2022-06-02 05:06:45 +03:00
|
|
|
AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector) ->
|
2022-06-24 09:58:25 +03:00
|
|
|
CPS.WriterT IR.R.TableRelationships m IR.E.Expression
|
|
|
|
translateBoolExp columnRelationshipReversePath sourceTableName = \case
|
2022-06-02 05:06:45 +03:00
|
|
|
BoolAnd xs ->
|
2022-06-24 09:58:25 +03:00
|
|
|
mkIfZeroOrMany IR.E.And <$> traverse (translateBoolExp columnRelationshipReversePath sourceTableName) xs
|
2022-06-02 05:06:45 +03:00
|
|
|
BoolOr xs ->
|
2022-06-24 09:58:25 +03:00
|
|
|
mkIfZeroOrMany IR.E.Or <$> traverse (translateBoolExp columnRelationshipReversePath sourceTableName) xs
|
2022-06-02 05:06:45 +03:00
|
|
|
BoolNot x ->
|
2022-06-24 09:58:25 +03:00
|
|
|
IR.E.Not <$> (translateBoolExp columnRelationshipReversePath sourceTableName) x
|
2022-06-02 05:06:45 +03:00
|
|
|
BoolFld (AVColumn c xs) ->
|
2022-06-24 09:58:25 +03:00
|
|
|
lift $ mkIfZeroOrMany IR.E.And <$> traverse (translateOp columnRelationshipReversePath (ciColumn c)) xs
|
|
|
|
BoolFld (AVRelationship relationshipInfo boolExp) -> do
|
|
|
|
let relationshipName = IR.R.mkRelationshipName $ riName relationshipInfo
|
|
|
|
let targetTable = riRTable relationshipInfo
|
|
|
|
let relationshipType = case riType relationshipInfo of
|
|
|
|
ObjRel -> IR.R.ObjectRelationship
|
|
|
|
ArrRel -> IR.R.ArrayRelationship
|
|
|
|
recordTableRelationship
|
|
|
|
sourceTableName
|
|
|
|
relationshipName
|
|
|
|
IR.R.Relationship
|
|
|
|
{ _rTargetTable = targetTable,
|
|
|
|
_rRelationshipType = relationshipType,
|
|
|
|
_rColumnMapping = riMapping relationshipInfo
|
|
|
|
}
|
|
|
|
translateBoolExp (relationshipName : columnRelationshipReversePath) targetTable boolExp
|
2022-06-02 05:06:45 +03:00
|
|
|
BoolExists _ ->
|
2022-06-24 09:58:25 +03:00
|
|
|
lift $ throw400 NotSupported "The BoolExists expression type is not supported by the Data Connector backend"
|
|
|
|
where
|
|
|
|
-- Makes an 'IR.E.Expression' like 'IR.E.And' if there is zero or many input expressions otherwise
|
|
|
|
-- just returns the singleton expression. This helps remove redundant 'IE.E.And' etcs from the expression.
|
|
|
|
mkIfZeroOrMany :: ([IR.E.Expression] -> IR.E.Expression) -> [IR.E.Expression] -> IR.E.Expression
|
|
|
|
mkIfZeroOrMany mk = \case
|
|
|
|
[singleExp] -> singleExp
|
|
|
|
zeroOrManyExps -> mk zeroOrManyExps
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateOp ::
|
2022-06-24 09:58:25 +03:00
|
|
|
[IR.R.RelationshipName] ->
|
2022-06-02 05:06:45 +03:00
|
|
|
IR.C.Name ->
|
|
|
|
OpExpG 'DataConnector (UnpreparedValue 'DataConnector) ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m IR.E.Expression
|
2022-06-24 09:58:25 +03:00
|
|
|
translateOp columnRelationshipReversePath columnName opExp = do
|
2022-06-02 05:06:45 +03:00
|
|
|
preparedOpExp <- traverse prepareLiterals $ opExp
|
|
|
|
case preparedOpExp of
|
|
|
|
AEQ _ (IR.S.ValueLiteral value) ->
|
|
|
|
pure $ mkApplyBinaryComparisonOperatorToScalar IR.E.Equal value
|
|
|
|
AEQ _ (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for AEQ operator"
|
|
|
|
ANE _ (IR.S.ValueLiteral value) ->
|
|
|
|
pure . IR.E.Not $ mkApplyBinaryComparisonOperatorToScalar IR.E.Equal value
|
|
|
|
ANE _ (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for ANE operator"
|
|
|
|
AGT (IR.S.ValueLiteral value) ->
|
|
|
|
pure $ mkApplyBinaryComparisonOperatorToScalar IR.E.GreaterThan value
|
|
|
|
AGT (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for AGT operator"
|
|
|
|
ALT (IR.S.ValueLiteral value) ->
|
|
|
|
pure $ mkApplyBinaryComparisonOperatorToScalar IR.E.LessThan value
|
|
|
|
ALT (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for ALT operator"
|
|
|
|
AGTE (IR.S.ValueLiteral value) ->
|
|
|
|
pure $ mkApplyBinaryComparisonOperatorToScalar IR.E.GreaterThanOrEqual value
|
|
|
|
AGTE (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for AGTE operator"
|
|
|
|
ALTE (IR.S.ValueLiteral value) ->
|
|
|
|
pure $ mkApplyBinaryComparisonOperatorToScalar IR.E.LessThanOrEqual value
|
|
|
|
ALTE (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for ALTE operator"
|
|
|
|
ANISNULL ->
|
2022-06-24 09:58:25 +03:00
|
|
|
pure $ IR.E.ApplyUnaryComparisonOperator IR.E.IsNull currentComparisonColumn
|
2022-06-02 05:06:45 +03:00
|
|
|
ANISNOTNULL ->
|
2022-06-24 09:58:25 +03:00
|
|
|
pure $ IR.E.Not (IR.E.ApplyUnaryComparisonOperator IR.E.IsNull currentComparisonColumn)
|
2022-06-02 05:06:45 +03:00
|
|
|
AIN literal -> pure $ inOperator literal
|
|
|
|
ANIN literal -> pure . IR.E.Not $ inOperator literal
|
|
|
|
CEQ rootOrCurrentColumn ->
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.Equal rootOrCurrentColumn
|
|
|
|
CNE rootOrCurrentColumn ->
|
|
|
|
IR.E.Not <$> mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.Equal rootOrCurrentColumn
|
|
|
|
CGT rootOrCurrentColumn ->
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.GreaterThan rootOrCurrentColumn
|
|
|
|
CLT rootOrCurrentColumn ->
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.LessThan rootOrCurrentColumn
|
|
|
|
CGTE rootOrCurrentColumn ->
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.GreaterThanOrEqual rootOrCurrentColumn
|
|
|
|
CLTE rootOrCurrentColumn ->
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.LessThanOrEqual rootOrCurrentColumn
|
|
|
|
ALIKE _literal ->
|
|
|
|
throw400 NotSupported "The ALIKE operator is not supported by the Data Connector backend"
|
|
|
|
ANLIKE _literal ->
|
|
|
|
throw400 NotSupported "The ANLIKE operator is not supported by the Data Connector backend"
|
|
|
|
ACast _literal ->
|
|
|
|
throw400 NotSupported "The ACast operator is not supported by the Data Connector backend"
|
|
|
|
where
|
2022-06-24 09:58:25 +03:00
|
|
|
currentComparisonColumn :: IR.E.ComparisonColumn
|
|
|
|
currentComparisonColumn = IR.E.ComparisonColumn (reverse columnRelationshipReversePath) columnName
|
|
|
|
|
2022-06-02 05:06:45 +03:00
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn :: IR.E.BinaryComparisonOperator -> RootOrCurrentColumn 'DataConnector -> m IR.E.Expression
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn operator (RootOrCurrentColumn rootOrCurrent otherColumnName) = do
|
2022-06-24 09:58:25 +03:00
|
|
|
let columnPath = case rootOrCurrent of
|
|
|
|
IsRoot -> []
|
|
|
|
IsCurrent -> (reverse columnRelationshipReversePath)
|
|
|
|
pure $ IR.E.ApplyBinaryComparisonOperator operator currentComparisonColumn (IR.E.AnotherColumn $ IR.E.ComparisonColumn columnPath otherColumnName)
|
2022-06-02 05:06:45 +03:00
|
|
|
|
|
|
|
inOperator :: IR.S.Literal -> IR.E.Expression
|
|
|
|
inOperator literal =
|
|
|
|
let values = case literal of
|
2022-06-24 09:58:25 +03:00
|
|
|
IR.S.ArrayLiteral array -> array
|
|
|
|
IR.S.ValueLiteral value -> [value]
|
|
|
|
in IR.E.ApplyBinaryArrayComparisonOperator IR.E.In currentComparisonColumn values
|
2022-06-02 05:06:45 +03:00
|
|
|
|
|
|
|
mkApplyBinaryComparisonOperatorToScalar :: IR.E.BinaryComparisonOperator -> IR.S.Value -> IR.E.Expression
|
|
|
|
mkApplyBinaryComparisonOperatorToScalar operator value =
|
2022-06-24 09:58:25 +03:00
|
|
|
IR.E.ApplyBinaryComparisonOperator operator currentComparisonColumn (IR.E.ScalarValue value)
|
2022-04-08 09:48:37 +03:00
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
-- | Validate if a 'IR.Q' contains any relationships.
|
2022-06-24 09:58:25 +03:00
|
|
|
queryHasRelations :: IR.Q.QueryRequest -> Bool
|
|
|
|
queryHasRelations IR.Q.QueryRequest {..} = _qrTableRelationships /= mempty
|