コード例 #1
0
        public void SqlBetweenScalarExpressionTest()
        {
            SqlBetweenScalarExpression threeBetweenFourAndFive = SqlBetweenScalarExpression.Create(
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(4)),
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(3)),
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(5)),
                not: false);

            AssertEvaluation(CosmosBoolean.Create(true), threeBetweenFourAndFive);

            SqlBetweenScalarExpression threeNotBetweenFourAndFive = SqlBetweenScalarExpression.Create(
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(4)),
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(3)),
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(5)),
                not: true);

            AssertEvaluation(CosmosBoolean.Create(false), threeNotBetweenFourAndFive);

            SqlBetweenScalarExpression trueBetweenTrueAndTrueNested = SqlBetweenScalarExpression.Create(
                SqlLiteralScalarExpression.Create(SqlBooleanLiteral.Create(true)),
                threeBetweenFourAndFive,
                threeBetweenFourAndFive,
                not: false);

            AssertEvaluation(CosmosBoolean.Create(true), trueBetweenTrueAndTrueNested);
        }
コード例 #2
0
            public static SqlScalarExpression GetCaseInsensitiveExpression(Expression expression)
            {
                if (expression is ConstantExpression inputExpression &&
                    inputExpression.Value is StringComparison comparisonValue &&
                    IgnoreCaseComparisons.Contains(comparisonValue))
                {
                    SqlBooleanLiteral literal = SqlBooleanLiteral.Create(true);
                    return(SqlLiteralScalarExpression.Create(literal));
                }

                return(null);
            }
コード例 #3
0
            public static SqlScalarExpression GetCaseSensitivityExpression(Expression expression)
            {
                if (expression is ConstantExpression inputExpression &&
                    inputExpression.Value is StringComparison comparisonValue)
                {
                    if (SensitiveCaseComparisons.Contains(comparisonValue))
                    {
                        return(SqlLiteralScalarExpression.Create(SqlBooleanLiteral.Create(false)));
                    }
                    else if (IgnoreCaseComparisons.Contains(comparisonValue))
                    {
                        return(SqlLiteralScalarExpression.Create(SqlBooleanLiteral.Create(true)));
                    }
                }

                return(null);
            }
コード例 #4
0
        public override SqlObject VisitLiteralScalarExpression([NotNull] sqlParser.LiteralScalarExpressionContext context)
        {
            Contract.Requires(context != null);
            Contract.Requires(context.ChildCount == 1);
            Contract.Requires(context.children[0].ChildCount == 1);

            TerminalNodeImpl terminalNode = (TerminalNodeImpl)(context.children[0].GetChild(0));

            SqlLiteralScalarExpression sqlLiteralScalarExpression;

            switch (terminalNode.Symbol.Type)
            {
            case sqlParser.STRING_LITERAL:
                string value = CstToAstVisitor.GetStringValueFromNode(terminalNode);
                sqlLiteralScalarExpression = SqlLiteralScalarExpression.Create(SqlStringLiteral.Create(value));
                break;

            case sqlParser.NUMERIC_LITERAL:
                Number64 number64 = CstToAstVisitor.GetNumber64ValueFromNode(terminalNode);
                sqlLiteralScalarExpression = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(number64));
                break;

            case sqlParser.K_TRUE:
                sqlLiteralScalarExpression = SqlLiteralScalarExpression.Create(SqlBooleanLiteral.Create(true));
                break;

            case sqlParser.K_FALSE:
                sqlLiteralScalarExpression = SqlLiteralScalarExpression.Create(SqlBooleanLiteral.Create(false));
                break;

            case sqlParser.K_NULL:
                sqlLiteralScalarExpression = SqlLiteralScalarExpression.Create(SqlNullLiteral.Create());
                break;

            case sqlParser.K_UNDEFINED:
                sqlLiteralScalarExpression = SqlLiteralScalarExpression.Create(SqlUndefinedLiteral.Create());
                break;

            default:
                throw new ArgumentOutOfRangeException($"Unknown symbol type: {terminalNode.Symbol.Type}");
            }

            return(sqlLiteralScalarExpression);
        }
コード例 #5
0
        public override SqlObject VisitLiteral([NotNull] sqlParser.LiteralContext context)
        {
            TerminalNodeImpl terminalNode = (TerminalNodeImpl)context.children[0];

            SqlLiteral sqlLiteral;

            switch (terminalNode.Symbol.Type)
            {
            case sqlParser.STRING_LITERAL:
                string value = CstToAstVisitor.GetStringValueFromNode(terminalNode);
                sqlLiteral = SqlStringLiteral.Create(value);
                break;

            case sqlParser.NUMERIC_LITERAL:
                Number64 number64 = CstToAstVisitor.GetNumber64ValueFromNode(terminalNode);
                sqlLiteral = SqlNumberLiteral.Create(number64);
                break;

            case sqlParser.K_TRUE:
                sqlLiteral = SqlBooleanLiteral.Create(true);
                break;

            case sqlParser.K_FALSE:
                sqlLiteral = SqlBooleanLiteral.Create(false);
                break;

            case sqlParser.K_NULL:
                sqlLiteral = SqlNullLiteral.Create();
                break;

            case sqlParser.K_UNDEFINED:
                sqlLiteral = SqlUndefinedLiteral.Create();
                break;

            default:
                throw new ArgumentOutOfRangeException($"Unknown symbol type: {terminalNode.Symbol.Type}");
            }

            return(sqlLiteral);
        }
コード例 #6
0
        /// <summary>
        /// Constructs <see cref="SqlScalarExpression"/> from a geometry <see cref="JToken"/>.
        /// </summary>
        /// <param name="jToken">Json token.</param>
        /// <returns>Instance of <see cref="SqlScalarExpression"/>.</returns>
        private static SqlScalarExpression FromJToken(JToken jToken)
        {
            switch (jToken.Type)
            {
            case JTokenType.Array:
                return(SqlArrayCreateScalarExpression.Create(jToken.Select(FromJToken).ToArray()));

            case JTokenType.Boolean:
                return(SqlLiteralScalarExpression.Create(SqlBooleanLiteral.Create(jToken.Value <bool>())));

            case JTokenType.Null:
                return(SqlLiteralScalarExpression.SqlNullLiteralScalarExpression);

            case JTokenType.String:
                return(SqlLiteralScalarExpression.Create(SqlStringLiteral.Create(jToken.Value <string>())));

            case JTokenType.Object:

                SqlObjectProperty[] properties =
                    ((JObject)jToken).Properties()
                    .Select(
                        p =>
                        SqlObjectProperty.Create(
                            SqlPropertyName.Create(p.Name),
                            FromJToken(p.Value)))
                    .ToArray();

                return(SqlObjectCreateScalarExpression.Create(properties));

            case JTokenType.Float:
            case JTokenType.Integer:
                SqlNumberLiteral sqlNumberLiteral = SqlNumberLiteral.Create(jToken.Value <double>());
                return(SqlLiteralScalarExpression.Create(sqlNumberLiteral));

            default:
                throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.UnexpectedTokenType, jToken.Type));
            }
        }
コード例 #7
0
        /// <summary>
        /// Converts a JToken to a semantically equivalent SqlScalarExpression.
        /// </summary>
        /// <param name="token">The JToken to convert.</param>
        /// <returns>The semantically equivalent SqlScalarExpression.</returns>
        public static SqlScalarExpression Convert(JToken token)
        {
            if (token == null)
            {
                return(Undefined);
            }

            switch (token.Type)
            {
            case JTokenType.Array:
            {
                List <SqlScalarExpression> items = new List <SqlScalarExpression>();
                foreach (JToken element in token)
                {
                    items.Add(JTokenToSqlScalarExpression.Convert(element));
                }

                return(SqlArrayCreateScalarExpression.Create(items.ToArray()));
            }

            case JTokenType.Boolean:
            {
                SqlBooleanLiteral literal = SqlBooleanLiteral.Create(token.ToObject <bool>());
                return(SqlLiteralScalarExpression.Create(literal));
            }

            case JTokenType.Null:
            {
                SqlNullLiteral literal = SqlNullLiteral.Singleton;
                return(SqlLiteralScalarExpression.Create(literal));
            }

            case JTokenType.Integer:
            case JTokenType.Float:
            {
                SqlNumberLiteral literal = SqlNumberLiteral.Create(token.ToObject <double>());
                return(SqlLiteralScalarExpression.Create(literal));
            }

            case JTokenType.Object:
            {
                List <SqlObjectProperty> properties = new List <SqlObjectProperty>();

                foreach (JProperty prop in (JToken)token)
                {
                    SqlPropertyName     name       = SqlPropertyName.Create(prop.Name);
                    JToken              value      = prop.Value;
                    SqlScalarExpression expression = JTokenToSqlScalarExpression.Convert(value);
                    SqlObjectProperty   property   = SqlObjectProperty.Create(name, expression);
                    properties.Add(property);
                }

                return(SqlObjectCreateScalarExpression.Create(properties.ToArray()));
            }

            case JTokenType.String:
            {
                SqlStringLiteral literal = SqlStringLiteral.Create(token.ToObject <string>());
                return(SqlLiteralScalarExpression.Create(literal));
            }

            default:
                throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, "Unsupported JsonType {0}", token.Type));
            }
        }