Esempio n. 1
0
 public override SqlObject Visit(SqlConditionalScalarExpression sqlConditionalScalarExpression)
 {
     return(SqlConditionalScalarExpression.Create(
                sqlConditionalScalarExpression.ConditionExpression.Accept(this) as SqlScalarExpression,
                sqlConditionalScalarExpression.FirstExpression.Accept(this) as SqlScalarExpression,
                sqlConditionalScalarExpression.SecondExpression.Accept(this) as SqlScalarExpression));
 }
 public override SqlObject Visit(SqlConditionalScalarExpression sqlConditionalScalarExpression)
 {
     return(SqlConditionalScalarExpression.Create(
                sqlConditionalScalarExpression.Condition.Accept(this) as SqlScalarExpression,
                sqlConditionalScalarExpression.Consequent.Accept(this) as SqlScalarExpression,
                sqlConditionalScalarExpression.Alternative.Accept(this) as SqlScalarExpression));
 }
Esempio n. 3
0
        public override CosmosElement Visit(SqlConditionalScalarExpression scalarExpression, CosmosElement document)
        {
            CosmosElement condition = scalarExpression.Condition.Accept(this, document);
            CosmosElement first     = scalarExpression.Consequent.Accept(this, document);
            CosmosElement second    = scalarExpression.Alternative.Accept(this, document);

            return((condition is CosmosBoolean cosmosBoolean && cosmosBoolean.Value) ? first : second);
        }
Esempio n. 4
0
        public override int Visit(SqlConditionalScalarExpression sqlConditionalScalarExpression)
        {
            int hashCode = SqlConditionalScalarExpressionHashCode;

            hashCode = CombineHashes(hashCode, sqlConditionalScalarExpression.ConditionExpression.Accept(this));
            hashCode = CombineHashes(hashCode, sqlConditionalScalarExpression.FirstExpression.Accept(this));
            hashCode = CombineHashes(hashCode, sqlConditionalScalarExpression.SecondExpression.Accept(this));
            return(hashCode);
        }
        public override int Visit(SqlConditionalScalarExpression sqlConditionalScalarExpression)
        {
            int hashCode = SqlConditionalScalarExpressionHashCode;

            hashCode = CombineHashes(hashCode, sqlConditionalScalarExpression.Condition.Accept(this));
            hashCode = CombineHashes(hashCode, sqlConditionalScalarExpression.Consequent.Accept(this));
            hashCode = CombineHashes(hashCode, sqlConditionalScalarExpression.Alternative.Accept(this));
            return(hashCode);
        }
 public override void Visit(SqlConditionalScalarExpression sqlConditionalScalarExpression)
 {
     this.writer.Write('(');
     sqlConditionalScalarExpression.Condition.Accept(this);
     this.writer.Write(" ? ");
     sqlConditionalScalarExpression.Consequent.Accept(this);
     this.writer.Write(" : ");
     sqlConditionalScalarExpression.Alternative.Accept(this);
     this.writer.Write(')');
 }
Esempio n. 7
0
 public override void Visit(SqlConditionalScalarExpression sqlConditionalScalarExpression)
 {
     this.writer.Write('(');
     sqlConditionalScalarExpression.ConditionExpression.Accept(this);
     this.writer.Write(" ? ");
     sqlConditionalScalarExpression.FirstExpression.Accept(this);
     this.writer.Write(" : ");
     sqlConditionalScalarExpression.SecondExpression.Accept(this);
     this.writer.Write(')');
 }
        public override SqlObject VisitConditionalScalarExpression([NotNull] sqlParser.ConditionalScalarExpressionContext context)
        {
            Contract.Requires(context != null);
            // scalar_expression '?' scalar_expression ':' scalar_expression
            Contract.Requires(context.ChildCount == 5);

            SqlScalarExpression condition   = (SqlScalarExpression)this.Visit(context.scalar_expression(0));
            SqlScalarExpression consequent  = (SqlScalarExpression)this.Visit(context.scalar_expression(1));
            SqlScalarExpression alternative = (SqlScalarExpression)this.Visit(context.scalar_expression(2));

            return(SqlConditionalScalarExpression.Create(condition, consequent, alternative));
        }
        public void SqlConditionalScalarExpressionTest()
        {
            SqlLiteralScalarExpression nullLiteral      = SqlLiteralScalarExpression.Create(SqlNullLiteral.Singleton);
            SqlLiteralScalarExpression undefinedLiteral = SqlLiteralScalarExpression.Create(SqlUndefinedLiteral.Create());
            SqlLiteralScalarExpression five             = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(5));
            SqlLiteralScalarExpression three            = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(3));

            SqlConditionalScalarExpression nullConditionalThreeFive = SqlConditionalScalarExpression.Create(nullLiteral, three, five);

            AssertEvaluation(CosmosNumber64.Create(5), nullConditionalThreeFive);

            SqlConditionalScalarExpression undefinedCoalesceFive = SqlConditionalScalarExpression.Create(undefinedLiteral, three, five);

            AssertEvaluation(CosmosNumber64.Create(5), undefinedCoalesceFive);

            SqlLiteralScalarExpression     trueBoolean = SqlLiteralScalarExpression.Create(SqlBooleanLiteral.True);
            SqlConditionalScalarExpression trueConditionalThreeFive = SqlConditionalScalarExpression.Create(trueBoolean, three, five);

            AssertEvaluation(CosmosNumber64.Create(3), trueConditionalThreeFive);
        }
Esempio n. 10
0
                public override bool Visit(SqlConditionalScalarExpression scalarExpression)
                {
                    if (this.MatchesGroupByExpression(scalarExpression))
                    {
                        return(true);
                    }

                    if (!scalarExpression.Condition.Accept(this))
                    {
                        return(false);
                    }

                    if (!scalarExpression.Consequent.Accept(this))
                    {
                        return(false);
                    }

                    if (!scalarExpression.Alternative.Accept(this))
                    {
                        return(false);
                    }

                    return(true);
                }
        public override bool Visit(SqlConditionalScalarExpression first, SqlObject secondAsObject)
        {
            if (!(secondAsObject is SqlConditionalScalarExpression second))
            {
                return(false);
            }

            if (!Equals(first.Condition, second.Condition))
            {
                return(false);
            }

            if (!Equals(first.Consequent, second.Consequent))
            {
                return(false);
            }

            if (!Equals(first.Alternative, second.Alternative))
            {
                return(false);
            }

            return(true);
        }
Esempio n. 12
0
 public abstract void Visit(SqlConditionalScalarExpression sqlObject);
Esempio n. 13
0
 public abstract TResult Visit(SqlConditionalScalarExpression scalarExpression);
Esempio n. 14
0
 public abstract TOutput Visit(SqlConditionalScalarExpression sqlObject, TArg input);
 public abstract void Visit(SqlConditionalScalarExpression scalarExpression);
Esempio n. 16
0
 public override bool Visit(SqlConditionalScalarExpression sqlConditionalScalarExpression)
 {
     return(sqlConditionalScalarExpression.Condition.Accept(this) ||
            sqlConditionalScalarExpression.Consequent.Accept(this) ||
            sqlConditionalScalarExpression.Alternative.Accept(this));
 }
Esempio n. 17
0
        public static SqlScalarExpression Substitute(SqlScalarExpression replacement, SqlIdentifier toReplace, SqlScalarExpression into)
        {
            if (into == null)
            {
                return(null);
            }

            if (replacement == null)
            {
                throw new ArgumentNullException("replacement");
            }

            switch (into.Kind)
            {
            case SqlObjectKind.ArrayCreateScalarExpression:
            {
                SqlArrayCreateScalarExpression arrayExp = into as SqlArrayCreateScalarExpression;
                if (arrayExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlArrayCreateScalarExpression, got a " + into.GetType());
                }

                SqlScalarExpression[] items = new SqlScalarExpression[arrayExp.Items.Count];
                for (int i = 0; i < items.Length; i++)
                {
                    SqlScalarExpression item     = arrayExp.Items[i];
                    SqlScalarExpression replitem = Substitute(replacement, toReplace, item);
                    items[i] = replitem;
                }

                return(SqlArrayCreateScalarExpression.Create(items));
            }

            case SqlObjectKind.BinaryScalarExpression:
            {
                SqlBinaryScalarExpression binaryExp = into as SqlBinaryScalarExpression;
                if (binaryExp == null)
                {
                    throw new DocumentQueryException("Expected a BinaryScalarExpression, got a " + into.GetType());
                }

                SqlScalarExpression replleft  = Substitute(replacement, toReplace, binaryExp.LeftExpression);
                SqlScalarExpression replright = Substitute(replacement, toReplace, binaryExp.RightExpression);
                return(SqlBinaryScalarExpression.Create(binaryExp.OperatorKind, replleft, replright));
            }

            case SqlObjectKind.UnaryScalarExpression:
            {
                SqlUnaryScalarExpression unaryExp = into as SqlUnaryScalarExpression;
                if (unaryExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlUnaryScalarExpression, got a " + into.GetType());
                }

                SqlScalarExpression repl = Substitute(replacement, toReplace, unaryExp.Expression);
                return(SqlUnaryScalarExpression.Create(unaryExp.OperatorKind, repl));
            }

            case SqlObjectKind.LiteralScalarExpression:
            {
                return(into);
            }

            case SqlObjectKind.FunctionCallScalarExpression:
            {
                SqlFunctionCallScalarExpression funcExp = into as SqlFunctionCallScalarExpression;
                if (funcExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlFunctionCallScalarExpression, got a " + into.GetType());
                }

                SqlScalarExpression[] items = new SqlScalarExpression[funcExp.Arguments.Count];
                for (int i = 0; i < items.Length; i++)
                {
                    SqlScalarExpression item     = funcExp.Arguments[i];
                    SqlScalarExpression replitem = Substitute(replacement, toReplace, item);
                    items[i] = replitem;
                }

                return(SqlFunctionCallScalarExpression.Create(funcExp.Name, funcExp.IsUdf, items));
            }

            case SqlObjectKind.ObjectCreateScalarExpression:
            {
                SqlObjectCreateScalarExpression objExp = into as SqlObjectCreateScalarExpression;
                if (objExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlObjectCreateScalarExpression, got a " + into.GetType());
                }

                return(SqlObjectCreateScalarExpression.Create(
                           objExp
                           .Properties
                           .Select(prop => SqlObjectProperty.Create(prop.Name, Substitute(replacement, toReplace, prop.Expression)))));
            }

            case SqlObjectKind.MemberIndexerScalarExpression:
            {
                SqlMemberIndexerScalarExpression memberExp = into as SqlMemberIndexerScalarExpression;
                if (memberExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlMemberIndexerScalarExpression, got a " + into.GetType());
                }

                SqlScalarExpression replMember = Substitute(replacement, toReplace, memberExp.MemberExpression);
                SqlScalarExpression replIndex  = Substitute(replacement, toReplace, memberExp.IndexExpression);
                return(SqlMemberIndexerScalarExpression.Create(replMember, replIndex));
            }

            case SqlObjectKind.PropertyRefScalarExpression:
            {
                // This is the leaf of the recursion
                SqlPropertyRefScalarExpression propExp = into as SqlPropertyRefScalarExpression;
                if (propExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlPropertyRefScalarExpression, got a " + into.GetType());
                }

                if (propExp.MemberExpression == null)
                {
                    if (propExp.PropertyIdentifier.Value == toReplace.Value)
                    {
                        return(replacement);
                    }
                    else
                    {
                        return(propExp);
                    }
                }
                else
                {
                    SqlScalarExpression replMember = Substitute(replacement, toReplace, propExp.MemberExpression);
                    return(SqlPropertyRefScalarExpression.Create(replMember, propExp.PropertyIdentifier));
                }
            }

            case SqlObjectKind.ConditionalScalarExpression:
            {
                SqlConditionalScalarExpression conditionalExpression = (SqlConditionalScalarExpression)into;
                if (conditionalExpression == null)
                {
                    throw new ArgumentException();
                }

                SqlScalarExpression condition = Substitute(replacement, toReplace, conditionalExpression.ConditionExpression);
                SqlScalarExpression first     = Substitute(replacement, toReplace, conditionalExpression.FirstExpression);
                SqlScalarExpression second    = Substitute(replacement, toReplace, conditionalExpression.SecondExpression);

                return(SqlConditionalScalarExpression.Create(condition, first, second));
            }

            case SqlObjectKind.InScalarExpression:
            {
                SqlInScalarExpression inExpression = (SqlInScalarExpression)into;
                if (inExpression == null)
                {
                    throw new ArgumentException();
                }

                SqlScalarExpression expression = Substitute(replacement, toReplace, inExpression.Expression);

                SqlScalarExpression[] items = new SqlScalarExpression[inExpression.Items.Count];
                for (int i = 0; i < items.Length; i++)
                {
                    items[i] = Substitute(replacement, toReplace, inExpression.Items[i]);
                }

                return(SqlInScalarExpression.Create(expression, inExpression.Not, items));
            }

            default:
                throw new ArgumentOutOfRangeException("Unexpected Sql Scalar expression kind " + into.Kind);
            }
        }
        public static SqlScalarExpression Substitute(SqlScalarExpression replacement, SqlIdentifier toReplace, SqlScalarExpression into)
        {
            if (into == null)
            {
                return(null);
            }

            if (replacement == null)
            {
                throw new ArgumentNullException("replacement");
            }

            switch (into)
            {
            case SqlArrayCreateScalarExpression arrayExp:
            {
                SqlScalarExpression[] items = new SqlScalarExpression[arrayExp.Items.Length];
                for (int i = 0; i < items.Length; i++)
                {
                    SqlScalarExpression item     = arrayExp.Items[i];
                    SqlScalarExpression replitem = Substitute(replacement, toReplace, item);
                    items[i] = replitem;
                }

                return(SqlArrayCreateScalarExpression.Create(items));
            }

            case SqlBinaryScalarExpression binaryExp:
            {
                SqlScalarExpression replleft  = Substitute(replacement, toReplace, binaryExp.LeftExpression);
                SqlScalarExpression replright = Substitute(replacement, toReplace, binaryExp.RightExpression);
                return(SqlBinaryScalarExpression.Create(binaryExp.OperatorKind, replleft, replright));
            }

            case SqlUnaryScalarExpression unaryExp:
            {
                SqlScalarExpression repl = Substitute(replacement, toReplace, unaryExp.Expression);
                return(SqlUnaryScalarExpression.Create(unaryExp.OperatorKind, repl));
            }

            case SqlLiteralScalarExpression literalScalarExpression:
            {
                return(into);
            }

            case SqlFunctionCallScalarExpression funcExp:
            {
                SqlScalarExpression[] items = new SqlScalarExpression[funcExp.Arguments.Length];
                for (int i = 0; i < items.Length; i++)
                {
                    SqlScalarExpression item     = funcExp.Arguments[i];
                    SqlScalarExpression replitem = Substitute(replacement, toReplace, item);
                    items[i] = replitem;
                }

                return(SqlFunctionCallScalarExpression.Create(funcExp.Name, funcExp.IsUdf, items));
            }

            case SqlObjectCreateScalarExpression objExp:
            {
                return(SqlObjectCreateScalarExpression.Create(
                           objExp
                           .Properties
                           .Select(prop => SqlObjectProperty.Create(prop.Name, Substitute(replacement, toReplace, prop.Value)))
                           .ToImmutableArray()));
            }

            case SqlMemberIndexerScalarExpression memberExp:
            {
                SqlScalarExpression replMember = Substitute(replacement, toReplace, memberExp.Member);
                SqlScalarExpression replIndex  = Substitute(replacement, toReplace, memberExp.Indexer);
                return(SqlMemberIndexerScalarExpression.Create(replMember, replIndex));
            }

            case SqlPropertyRefScalarExpression propExp:
            {
                // This is the leaf of the recursion
                if (propExp.Member == null)
                {
                    if (propExp.Identifier.Value == toReplace.Value)
                    {
                        return(replacement);
                    }
                    else
                    {
                        return(propExp);
                    }
                }
                else
                {
                    SqlScalarExpression replMember = Substitute(replacement, toReplace, propExp.Member);
                    return(SqlPropertyRefScalarExpression.Create(replMember, propExp.Identifier));
                }
            }

            case SqlConditionalScalarExpression conditionalExpression:
            {
                SqlScalarExpression condition = Substitute(replacement, toReplace, conditionalExpression.Condition);
                SqlScalarExpression first     = Substitute(replacement, toReplace, conditionalExpression.Consequent);
                SqlScalarExpression second    = Substitute(replacement, toReplace, conditionalExpression.Alternative);

                return(SqlConditionalScalarExpression.Create(condition, first, second));
            }

            case SqlInScalarExpression inExpression:
            {
                SqlScalarExpression expression = Substitute(replacement, toReplace, inExpression.Needle);

                SqlScalarExpression[] items = new SqlScalarExpression[inExpression.Haystack.Length];
                for (int i = 0; i < items.Length; i++)
                {
                    items[i] = Substitute(replacement, toReplace, inExpression.Haystack[i]);
                }

                return(SqlInScalarExpression.Create(expression, inExpression.Not, items));
            }

            default:
                throw new ArgumentOutOfRangeException("Unexpected Sql Scalar expression kind " + into.GetType());
            }
        }
Esempio n. 19
0
 public abstract TResult Visit(SqlConditionalScalarExpression sqlObject);