public override void Visit(SqlMemberIndexerScalarExpression sqlMemberIndexerScalarExpression)
 {
     sqlMemberIndexerScalarExpression.Member.Accept(this);
     this.writer.Write("[");
     sqlMemberIndexerScalarExpression.Indexer.Accept(this);
     this.writer.Write("]");
 }
Beispiel #2
0
        public override CosmosElement Visit(SqlMemberIndexerScalarExpression scalarExpression, CosmosElement document)
        {
            CosmosElement member = scalarExpression.Member.Accept(this, document);
            CosmosElement index  = scalarExpression.Indexer.Accept(this, document);

            return(IndexIntoMember(member, index));
        }
        public override int Visit(SqlMemberIndexerScalarExpression sqlMemberIndexerScalarExpression)
        {
            int hashCode = SqlMemberIndexerScalarExpressionHashCode;

            hashCode = CombineHashes(hashCode, sqlMemberIndexerScalarExpression.Member.Accept(this));
            hashCode = CombineHashes(hashCode, sqlMemberIndexerScalarExpression.Indexer.Accept(this));
            return(hashCode);
        }
        private SqlScalarExpression GetLastMemberIndexerToken(SqlMemberIndexerScalarExpression memberIndexer)
        {
            SqlScalarExpression last;

            if (!(memberIndexer.Indexer is SqlMemberIndexerScalarExpression memberIndexerExpression))
            {
                last = memberIndexer.Indexer;
            }
Beispiel #5
0
                public override bool Visit(SqlMemberIndexerScalarExpression scalarExpression)
                {
                    if (this.MatchesGroupByExpression(scalarExpression))
                    {
                        return(true);
                    }

                    return(false);
                }
        public override SqlObject VisitMemberIndexerScalarExpression([NotNull] sqlParser.MemberIndexerScalarExpressionContext context)
        {
            Contract.Requires(context != null);
            // primary_expression '[' scalar_expression ']'

            SqlScalarExpression memberExpression = (SqlScalarExpression)this.Visit(context.primary_expression());
            SqlScalarExpression indexExpression  = (SqlScalarExpression)this.Visit(context.scalar_expression());

            return(SqlMemberIndexerScalarExpression.Create(memberExpression, indexExpression));
        }
            protected override SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context)
            {
                if (methodCallExpression.Object != null && methodCallExpression.Arguments.Count == 1)
                {
                    SqlScalarExpression memberExpression = ExpressionToSql.VisitScalarExpression(methodCallExpression.Object, context);
                    SqlScalarExpression indexExpression  = ExpressionToSql.VisitScalarExpression(methodCallExpression.Arguments[0], context);

                    return(SqlMemberIndexerScalarExpression.Create(memberExpression, indexExpression));
                }

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

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

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

            return(true);
        }
        private static SqlScalarExpression GenerateMemberIndexerScalarExpressionFromPath(Path path)
        {
            if (path.Length < 1)
            {
                throw new ArgumentException($"{nameof(path)} is too short.");
            }

            if (!(path.First() is StringPathToken rootToken))
            {
                throw new ArgumentException($"{nameof(path)} did not start with a string.");
            }

            SqlScalarExpression rootExpression = SqlPropertyRefScalarExpression.Create(
                member: null,
                identifier: SqlIdentifier.Create(rootToken.PropertyName));

            foreach (PathToken token in path.Skip(1))
            {
                SqlLiteralScalarExpression memberIndexer;
                switch (token)
                {
                case StringPathToken stringPathToken:
                    memberIndexer = SqlLiteralScalarExpression.Create(
                        SqlStringLiteral.Create(
                            stringPathToken.PropertyName));
                    break;

                case IntegerPathToken integerPathToken:
                    memberIndexer = SqlLiteralScalarExpression.Create(
                        SqlNumberLiteral.Create(
                            integerPathToken.Index));
                    break;

                default:
                    throw new ArgumentException($"Unknown token type: {token.GetType()}; {token}");
                }

                rootExpression = SqlMemberIndexerScalarExpression.Create(rootExpression, memberIndexer);
            }

            return(rootExpression);
        }
 public override SqlObject Visit(SqlMemberIndexerScalarExpression sqlMemberIndexerScalarExpression)
 {
     return(SqlMemberIndexerScalarExpression.Create(
                sqlMemberIndexerScalarExpression.Member.Accept(this) as SqlScalarExpression,
                sqlMemberIndexerScalarExpression.Indexer.Accept(this) as SqlScalarExpression));
 }
 public abstract void Visit(SqlMemberIndexerScalarExpression scalarExpression);
Beispiel #12
0
 public abstract TOutput Visit(SqlMemberIndexerScalarExpression sqlObject, TArg input);
Beispiel #13
0
 public override bool Visit(SqlMemberIndexerScalarExpression sqlMemberIndexerScalarExpression)
 {
     return(sqlMemberIndexerScalarExpression.Member.Accept(this) ||
            sqlMemberIndexerScalarExpression.Indexer.Accept(this));
 }
Beispiel #14
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);
            }
        }
Beispiel #15
0
 public abstract TResult Visit(SqlMemberIndexerScalarExpression sqlObject);
Beispiel #16
0
 public abstract void Visit(SqlMemberIndexerScalarExpression sqlObject);
        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());
            }
        }