2022-01-11 01:54:51 +03:00
|
|
|
-- | MSSQL DDL BoolExp
|
|
|
|
--
|
|
|
|
-- How to parse the boolean expressions and operations relevant for MSSQL.
|
2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.Backends.MSSQL.DDL.BoolExp
|
|
|
|
( parseBoolExpOperations,
|
|
|
|
)
|
|
|
|
where
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-06-15 18:05:41 +03:00
|
|
|
import Data.Aeson qualified as J
|
2022-06-08 18:31:28 +03:00
|
|
|
import Data.Aeson.Key qualified as K
|
|
|
|
import Data.Aeson.KeyMap qualified as KM
|
2021-06-15 18:05:41 +03:00
|
|
|
import Data.Text qualified as T
|
2021-07-28 11:09:32 +03:00
|
|
|
import Data.Text.Extended (dquote, toTxt, (<<>))
|
2021-11-26 16:47:12 +03:00
|
|
|
import Hasura.Backends.MSSQL.Types.Internal hiding (ColumnType)
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2021-02-23 20:37:27 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.BoolExp
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.SQL.Backend
|
|
|
|
import Hasura.SQL.Types
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
parseBoolExpOperations ::
|
|
|
|
forall m v.
|
|
|
|
(MonadError QErr m) => -- , TableCoreInfoRM 'MSSQL m)
|
|
|
|
ValueParser 'MSSQL m v ->
|
2023-02-24 15:31:04 +03:00
|
|
|
FieldInfoMap (FieldInfo 'MSSQL) ->
|
2021-02-23 20:37:27 +03:00
|
|
|
FieldInfoMap (FieldInfo 'MSSQL) ->
|
2021-07-28 11:09:32 +03:00
|
|
|
ColumnReference 'MSSQL ->
|
2021-02-23 20:37:27 +03:00
|
|
|
J.Value ->
|
|
|
|
m [OpExpG 'MSSQL v]
|
2023-02-24 15:31:04 +03:00
|
|
|
parseBoolExpOperations rhsParser _rootTableFieldInfoMap _fields columnRef value =
|
2021-07-28 11:09:32 +03:00
|
|
|
withPathK (toTxt columnRef) $ parseOperations (columnReferenceType columnRef) value
|
2021-02-23 20:37:27 +03:00
|
|
|
where
|
|
|
|
parseWithTy ty = rhsParser (CollectableTypeScalar ty)
|
|
|
|
|
|
|
|
parseOperations :: ColumnType 'MSSQL -> J.Value -> m [OpExpG 'MSSQL v]
|
|
|
|
parseOperations columnType = \case
|
2022-06-08 18:31:28 +03:00
|
|
|
J.Object o -> mapM (parseOperation columnType . first K.toText) $ KM.toList o
|
2021-02-23 20:37:27 +03:00
|
|
|
v -> pure . AEQ False <$> parseWithTy columnType v
|
|
|
|
|
|
|
|
parseOperation :: ColumnType 'MSSQL -> (Text, J.Value) -> m (OpExpG 'MSSQL v)
|
|
|
|
parseOperation columnType (opStr, val) = withPathK opStr $
|
|
|
|
case opStr of
|
2021-03-24 16:43:40 +03:00
|
|
|
"_eq" -> parseEq
|
|
|
|
"$eq" -> parseEq
|
|
|
|
"_neq" -> parseNeq
|
|
|
|
"$neq" -> parseNeq
|
|
|
|
"$in" -> parseIn
|
|
|
|
"_in" -> parseIn
|
|
|
|
"$nin" -> parseNin
|
|
|
|
"_nin" -> parseNin
|
|
|
|
"_gt" -> parseGt
|
|
|
|
"$gt" -> parseGt
|
|
|
|
"_lt" -> parseLt
|
|
|
|
"$lt" -> parseLt
|
|
|
|
"_gte" -> parseGte
|
|
|
|
"$gte" -> parseGte
|
|
|
|
"_lte" -> parseLte
|
|
|
|
"$lte" -> parseLte
|
|
|
|
"$like" -> parseLike
|
|
|
|
"_like" -> parseLike
|
|
|
|
"$nlike" -> parseNlike
|
|
|
|
"_nlike" -> parseNlike
|
2023-03-30 16:45:12 +03:00
|
|
|
"$is_null" -> parseIsNull
|
|
|
|
"_is_null" -> parseIsNull
|
2021-03-25 20:50:08 +03:00
|
|
|
"_st_contains" -> ABackendSpecific <$> parseGeometryOrGeographyOp ASTContains
|
|
|
|
"$st_contains" -> ABackendSpecific <$> parseGeometryOrGeographyOp ASTContains
|
|
|
|
"_st_equals" -> ABackendSpecific <$> parseGeometryOrGeographyOp ASTEquals
|
|
|
|
"$st_equals" -> ABackendSpecific <$> parseGeometryOrGeographyOp ASTEquals
|
|
|
|
"_st_intersects" -> ABackendSpecific <$> parseGeometryOrGeographyOp ASTIntersects
|
|
|
|
"$st_intersects" -> ABackendSpecific <$> parseGeometryOrGeographyOp ASTIntersects
|
|
|
|
"_st_overlaps" -> ABackendSpecific <$> parseGeometryOrGeographyOp ASTOverlaps
|
|
|
|
"$st_overlaps" -> ABackendSpecific <$> parseGeometryOrGeographyOp ASTOverlaps
|
|
|
|
"_st_within" -> ABackendSpecific <$> parseGeometryOrGeographyOp ASTWithin
|
|
|
|
"$st_within" -> ABackendSpecific <$> parseGeometryOrGeographyOp ASTWithin
|
|
|
|
"_st_crosses" -> ABackendSpecific <$> parseGeometryOp ASTCrosses
|
|
|
|
"$st_crosses" -> ABackendSpecific <$> parseGeometryOp ASTCrosses
|
|
|
|
"_st_touches" -> ABackendSpecific <$> parseGeometryOp ASTTouches
|
|
|
|
"$st_touches" -> ABackendSpecific <$> parseGeometryOp ASTTouches
|
2022-10-01 17:47:12 +03:00
|
|
|
x -> throw400 UnexpectedPayload $ "Unknown operator: " <> x
|
2021-02-23 20:37:27 +03:00
|
|
|
where
|
2021-07-28 11:09:32 +03:00
|
|
|
colTy = columnReferenceType columnRef
|
2021-03-19 15:42:09 +03:00
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
parseOne = parseWithTy columnType val
|
2021-03-19 15:42:09 +03:00
|
|
|
parseManyWithType ty = rhsParser (CollectableTypeArray ty) val
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-03-19 19:39:37 +03:00
|
|
|
parseEq = AEQ False <$> parseOne
|
|
|
|
parseNeq = ANE False <$> parseOne
|
|
|
|
parseIn = AIN <$> parseManyWithType colTy
|
|
|
|
parseNin = ANIN <$> parseManyWithType colTy
|
|
|
|
parseGt = AGT <$> parseOne
|
|
|
|
parseLt = ALT <$> parseOne
|
|
|
|
parseGte = AGTE <$> parseOne
|
|
|
|
parseLte = ALTE <$> parseOne
|
|
|
|
parseLike = guardType stringTypes >> ALIKE <$> parseOne
|
|
|
|
parseNlike = guardType stringTypes >> ANLIKE <$> parseOne
|
2023-03-30 16:45:12 +03:00
|
|
|
parseIsNull = bool ANISNOTNULL ANISNULL <$> parseVal
|
2021-03-19 15:42:09 +03:00
|
|
|
|
2021-03-24 16:43:40 +03:00
|
|
|
parseGeometryOp f =
|
|
|
|
guardType [GeometryType] >> f <$> parseOneNoSess colTy val
|
|
|
|
parseGeometryOrGeographyOp f =
|
|
|
|
guardType geoTypes >> f <$> parseOneNoSess colTy val
|
|
|
|
parseOneNoSess ty = rhsParser (CollectableTypeScalar ty)
|
|
|
|
|
2021-03-19 15:42:09 +03:00
|
|
|
guardType validTys =
|
|
|
|
unless (isScalarColumnWhere (`elem` validTys) colTy) $
|
|
|
|
throwError $
|
|
|
|
buildMsg colTy validTys
|
|
|
|
|
|
|
|
buildMsg ty expTys =
|
|
|
|
err400 UnexpectedPayload $
|
|
|
|
" is of type "
|
|
|
|
<> ty <<> "; this operator works only on columns of type "
|
|
|
|
<> T.intercalate "/" (map dquote expTys)
|
2023-03-30 16:45:12 +03:00
|
|
|
|
|
|
|
parseVal :: (J.FromJSON a) => m a
|
|
|
|
parseVal = decodeValue val
|