2021-02-23 20:37:27 +03:00
|
|
|
module Hasura.Backends.MSSQL.DDL.BoolExp where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import qualified Data.Aeson as J
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
2021-03-19 15:42:09 +03:00
|
|
|
import qualified Data.Text as T
|
|
|
|
|
|
|
|
import Data.Text.Extended (dquote, (<<>))
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
import Hasura.Backends.MSSQL.Instances.Types ()
|
|
|
|
import Hasura.Backends.MSSQL.Types hiding (ColumnType)
|
|
|
|
import Hasura.RQL.IR.BoolExp
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Error
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.SQL.Backend
|
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
|
|
|
parseBoolExpOperations
|
|
|
|
:: forall m v
|
|
|
|
. (MonadError QErr m) -- , TableCoreInfoRM 'MSSQL m)
|
|
|
|
=> ValueParser 'MSSQL m v
|
|
|
|
-> FieldInfoMap (FieldInfo 'MSSQL)
|
|
|
|
-> ColumnInfo 'MSSQL
|
|
|
|
-> J.Value
|
|
|
|
-> m [OpExpG 'MSSQL v]
|
|
|
|
parseBoolExpOperations rhsParser _fields columnInfo value =
|
|
|
|
withPathK (columnNameText $ pgiColumn columnInfo) $
|
|
|
|
parseOperations (pgiType columnInfo) value
|
|
|
|
where
|
|
|
|
parseWithTy ty = rhsParser (CollectableTypeScalar ty)
|
|
|
|
|
|
|
|
parseOperations :: ColumnType 'MSSQL -> J.Value -> m [OpExpG 'MSSQL v]
|
|
|
|
parseOperations columnType = \case
|
|
|
|
J.Object o -> mapM (parseOperation columnType) $ Map.toList o
|
|
|
|
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-19 15:42:09 +03:00
|
|
|
"_eq" -> parseEq
|
|
|
|
"$eq" -> parseEq
|
|
|
|
|
|
|
|
"_neq" -> parseNeq
|
|
|
|
"$neq" -> parseNeq
|
|
|
|
|
|
|
|
"$in" -> parseIn
|
|
|
|
"_in" -> parseIn
|
|
|
|
|
|
|
|
"$nin" -> parseNin
|
|
|
|
"_nin" -> parseNin
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-03-19 15:42:09 +03:00
|
|
|
"_gt" -> parseGt
|
|
|
|
"$gt" -> parseGt
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-03-19 15:42:09 +03:00
|
|
|
"_lt" -> parseLt
|
|
|
|
"$lt" -> parseLt
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-03-19 15:42:09 +03:00
|
|
|
"_gte" -> parseGte
|
|
|
|
"$gte" -> parseGte
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-03-19 15:42:09 +03:00
|
|
|
"_lte" -> parseLte
|
|
|
|
"$lte" -> parseLte
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-03-19 15:42:09 +03:00
|
|
|
"$like" -> parseLike
|
|
|
|
"_like" -> parseLike
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-03-19 15:42:09 +03:00
|
|
|
"$nlike" -> parseNlike
|
|
|
|
"_nlike" -> parseNlike
|
|
|
|
|
|
|
|
x -> throw400 UnexpectedPayload $ "Unknown operator : " <> x
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
where
|
2021-03-19 15:42:09 +03:00
|
|
|
colTy = pgiType columnInfo
|
|
|
|
|
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
|
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)
|