Beispiel #1
0
        protected override String Visit(ExpNode caller, ExpNode node)
        {
            if (node is ProjectExpression)
            {
                ProjectExpression e = (ProjectExpression)node;
                if (e.Projections.Count == 0)
                {
                    return(String.Format("{0}",
                                         this.Visit(e, e.Input)
                                         ));
                }
                else if (e.Projections.Count == 1)
                {
                    if (e.Projections[0] is PropertyExpression)
                    {
                        return(String.Format("{0}/{1}",
                                             this.Visit(e, e.Input),
                                             this.Visit(e, e.Projections[0])
                                             ));
                    }
                }
                else
                {
                    throw new Exception("More than 1 projection, invalid");
                }
            }
            else if (node is ServiceOperationExpression)
            {
                AstoriaTestLog.WriteLine("Calling Service Operation");
                ServiceOperationExpression e = (ServiceOperationExpression)node;

                string serviceOp = this.Visit(e, e.Input);
                AstoriaTestLog.WriteLine(serviceOp);
                StringBuilder sbParametersString = new StringBuilder();
                if (!serviceOp.Contains("?"))
                {
                    sbParametersString.Append("?");
                }
                for (int index = 0; index < e.Arguments.Length; index++)
                {
                    ServiceOperationParameterExpression parameter = e.Arguments[index];
                    if (index > 0)
                    {
                        sbParametersString.Append("&");
                    }
                    string strLiteralString = CreateKeyStringValue(parameter.ParameterValue);
                    sbParametersString.AppendFormat("{0}={1}", parameter.ParameterName, strLiteralString);
                }
                return(String.Format("{0}{1}", serviceOp, sbParametersString.ToString()));
            }
            else if (node is ScanExpression)
            {
                ScanExpression e = (ScanExpression)node;

                if (e.Input is VariableExpression && ((VariableExpression)e.Input).Variable is ResourceContainer)
                {
                    return(String.Format("{0}",
                                         this.Visit(e, e.Input)
                                         ));
                }
                throw new Exception("Unsupported on in scan expression");
            }
            else if (node is PredicateExpression)
            {
                PredicateExpression e   = (PredicateExpression)node;
                KeyExpression       key = e.Predicate as KeyExpression;
                if (key != null)
                {
                    return(String.Format("{0}({1})",
                                         this.Visit(e, e.Input),
                                         this.Visit(e, e.Predicate)
                                         ));
                }
                else
                {
                    return(String.Format("{0}?$filter={1}",
                                         this.Visit(e, e.Input),
                                         this.Visit(e, e.Predicate)
                                         ));
                }
            }
            else if (node is CountExpression)
            {
                CountExpression e      = (CountExpression)node;
                string          sCount = "";
                string          visit  = "";

                if (!e.IsInline)
                {
                    visit  = this.Visit(e, e.Input);
                    sCount = string.Format("{0}/$count", visit);
                }
                else
                {
                    visit = this.Visit(e, e.Input);

                    if (visit.IndexOf("?$") == -1)
                    {
                        sCount = string.Format("{0}?$inlinecount={1}", visit, e.CountKind);
                    }
                    else
                    {
                        sCount = string.Format("{0}&$inlinecount={1}", visit, e.CountKind);
                    }
                }
                return(sCount);
            }
            else if (node is NewExpression)
            {
                NewExpression e   = (NewExpression)node;
                string        uri = this.Visit(e, e.Input);
                ExpNode[]     pe  = new ExpNode[] { };

                List <ExpNode> nodes = new List <ExpNode>();
                foreach (ExpNode parameter in e.Arguments)
                {
                    nodes.Add(parameter as ExpNode);
                }
                pe = nodes.ToArray();
                string _select = String.Format("{0}", this.Visit(e, pe[0]));

                if (nodes.Count > 1)
                {
                    for (int j = 1; j < nodes.Count; j++)
                    {
                        _select = String.Format("{0},{1}", _select, this.Visit(e, pe[j]));
                    }
                }

                if (uri.IndexOf("?$") == -1)
                {
                    return(string.Format("{0}?$select={1}", uri, _select));
                }
                else
                {
                    return(string.Format("{0}&$select={1}", uri, _select));
                }
            }
            else if (node is MemberBindExpression)
            {
                return(this.Visit(node, ((MemberBindExpression)node).SourceProperty));
            }
            else if (node is TopExpression)
            {
                TopExpression e     = (TopExpression)node;
                string        visit = this.Visit(e, e.Input);

                if (visit.IndexOf("?$") == -1)
                {
                    return(string.Format("{0}?$top={1}", visit, e.Predicate));
                }
                else
                {
                    return(string.Format("{0}&$top={1}", visit, e.Predicate));
                }
            }
            else if (node is SkipExpression)
            {
                SkipExpression e     = (SkipExpression)node;
                string         visit = this.Visit(e, e.Input);

                if (visit.IndexOf("?$") == -1)
                {
                    return(string.Format("{0}?$skip={1}", visit, e.Predicate));
                }
                else
                {
                    return(string.Format("{0}&$skip={1}", visit, e.Predicate));
                }
            }
            else if (node is OrderByExpression)
            {
                OrderByExpression e          = (OrderByExpression)node;
                string            ordervalue = String.Empty;
                int i = 0;

                string visit = this.Visit(e, e.Input);
                if (e.ExcludeFromUri)
                {
                    return(visit);
                }

                string propVisit = this.Visit(e, e.PropertiesExp[0]);

                switch (e.AscDesc)
                {
                case true:
                    if (visit.IndexOf("?$") == -1)
                    {
                        ordervalue = String.Format("{0}?$orderby={1}", visit, propVisit);
                    }
                    else
                    {
                        ordervalue = String.Format("{0}&$orderby={1}", visit, propVisit);
                    }
                    break;

                case false:
                    if (visit.IndexOf("?$") == -1)
                    {
                        ordervalue = String.Format("{0}?$orderby={1} desc", visit, propVisit);
                    }
                    else
                    {
                        ordervalue = String.Format("{0}&$orderby={1} desc", visit, propVisit);
                    }
                    break;
                }
                ;

                if (e.PropertiesExp.Length > 0)
                {
                    for (i++; i < e.PropertiesExp.Length; i++)
                    {
                        String nextValue = this.Visit(e, e.PropertiesExp[i]);
                        nextValue = String.Format("{0}", nextValue);

                        switch (e.AscDesc)
                        {
                        case true:
                            ordervalue = String.Format("{0},{1}", ordervalue, nextValue);
                            break;

                        case false:
                            ordervalue = String.Format("{0},{1} desc", ordervalue, nextValue);
                            break;
                        }
                    }
                }
                return(ordervalue);
            }
            else if (node is ThenByExpression)
            {
                ThenByExpression e          = (ThenByExpression)node;
                string           ordervalue = String.Empty;
                int i = 0;

                string visit = this.Visit(e, e.Input);
                if (e.ExcludeFromUri)
                {
                    return(visit);
                }

                switch (e.AscDesc)
                {
                case true:
                    ordervalue = String.Format("{0},{1}", visit, e.Properties[0].Name);
                    break;

                case false:
                    ordervalue = String.Format("{0},{1} desc", visit, e.Properties[0].Name);
                    break;
                }

                if (e.Properties.Length > 0)
                {
                    for (i++; i < e.Properties.Length; i++)
                    {
                        String nextValue = e.Properties[i].Name;
                        nextValue = String.Format("{0}", nextValue);

                        switch (e.AscDesc)
                        {
                        case true:
                            ordervalue = String.Format("{0},{1}", ordervalue, nextValue);
                            break;

                        case false:
                            ordervalue = String.Format("{0},{1} desc", ordervalue, nextValue);
                            break;
                        }
                    }
                }
                return(ordervalue);
            }
            else if (node is ExpandExpression)
            {
                ExpandExpression e   = (ExpandExpression)node;
                string           uri = this.Visit(e, e.Input);

                string expand = String.Format("{0}", this.Visit(e, e.PropertiesExp[0]));

                if (e.Properties.Length > 1)
                {
                    for (int i = 1; i < e.Properties.Length; i++)
                    {
                        expand = String.Format("{0},{1}", expand, this.Visit(e, e.PropertiesExp[i]));
                    }
                }

                if (uri.IndexOf("?$") == -1)
                {
                    return(string.Format("{0}?$expand={1}", uri, expand));
                }
                else
                {
                    return(string.Format("{0}&$expand={1}", uri, expand));
                }
            }
            else if (node is NavigationExpression)
            {
                NavigationExpression e = (NavigationExpression)node;
                if (!e.IsLink)
                {
                    return(String.Format("{0}/{1}", this.Visit(e, e.Input), this.Visit(e, e.PropertyExp)));
                }
                else
                {
                    return(String.Format("{0}/{1}/$ref", this.Visit(e, e.Input), this.Visit(e, e.PropertyExp)));
                }
            }
            else if (node is KeyExpression)
            {
                KeyExpression key = node as KeyExpression;
                return(CreateKeyString(key));
            }
            else if (node is NestedPropertyExpression)
            {
                NestedPropertyExpression e = (NestedPropertyExpression)node;
                string enitySetname        = e.Name;
                string nestedProperty      = "";

                foreach (PropertyExpression p in e.PropertyExpressions)
                {
                    string interim = this.Visit(e, p);
                    //AstoriaTestLog.WriteLine(interim);
                    if (p.ValueOnly)
                    {
                        nestedProperty = String.Format("{0}/{1}/{2},", nestedProperty, enitySetname, interim + "/$value").TrimStart('/');
                    }
                    else
                    {
                        nestedProperty = String.Format("{0}/{1}/{2},", nestedProperty, enitySetname, interim).TrimStart('/');
                    }
                }

                return(nestedProperty.TrimEnd(',').Replace(",/", ","));
            }
            else if (node is PropertyExpression)
            {
                PropertyExpression e = (PropertyExpression)node;
                if (e.ValueOnly)
                {
                    return(e.Property.Name + "/$value");
                }
                else
                {
                    return(e.Property.Name);
                }
            }
            else if (node is VariableExpression)
            {
                VariableExpression e = (VariableExpression)node;
                return(e.Variable.Name);
            }
            if (node is LogicalExpression)
            {
                LogicalExpression e     = (LogicalExpression)node;
                string            left  = this.Visit(e, e.Left);
                string            right = null;

                if (e.Operator != LogicalOperator.Not)
                {
                    right = this.Visit(e, e.Right);
                }

                string logical;
                switch (e.Operator)
                {
                case LogicalOperator.And:
                    logical = string.Format("{0} and {1}", left, right);
                    break;

                case LogicalOperator.Or:
                    logical = string.Format("{0} or {1}", left, right);
                    break;

                case LogicalOperator.Not:
                    logical = string.Format("not {0}", left);
                    break;

                default:
                    throw new Exception("Unhandled Comparison Type: " + e.Operator);
                }
                return(logical);
            }
            else if (node is ComparisonExpression)
            {
                ComparisonExpression e = (ComparisonExpression)node;
                String left            = this.Visit(e, e.Left);
                String right           = this.Visit(e, e.Right);

                switch (e.Operator)
                {
                case ComparisonOperator.Equal:
                    return(String.Format("{0} eq {1}", left, right));

                case ComparisonOperator.NotEqual:
                    return(String.Format("{0} ne {1}", left, right));

                case ComparisonOperator.GreaterThan:
                    return(String.Format("{0} gt {1}", left, right));

                case ComparisonOperator.GreaterThanOrEqual:
                    return(String.Format("{0} ge {1}", left, right));

                case ComparisonOperator.LessThan:
                    return(String.Format("{0} lt {1}", left, right));

                case ComparisonOperator.LessThanOrEqual:
                    return(String.Format("{0} le {1}", left, right));

                default:
                    throw new Exception("Unhandled Comparison Type: " + e.Operator);
                }
                ;
            }
            else if (node is IsOfExpression || node is CastExpression)
            {
                ExpNode  target;
                NodeType targetType;
                string   operation;
                if (node is IsOfExpression)
                {
                    IsOfExpression e = (IsOfExpression)node;
                    operation  = "isof";
                    target     = e.Target;
                    targetType = e.TargetType;
                }
                else
                {
                    CastExpression e = (CastExpression)node;
                    operation  = "cast";
                    target     = e.Target;
                    targetType = e.TargetType;
                }

                string targetTypeName = targetType.FullName;
                if (targetType is PrimitiveType)
                {
                    targetTypeName = TypeData.FindForType(targetType.ClrType).GetEdmTypeName();
                }

                if (target == null)
                {
                    return(String.Format("{0}('{1}')", operation, targetTypeName));
                }
                else
                {
                    return(String.Format("{0}({1}, '{2}')", operation, this.Visit(node, target), targetTypeName));
                }
            }
            else if (node is ArithmeticExpression)
            {
                ArithmeticExpression e = (ArithmeticExpression)node;
                String left            = this.Visit(e, e.Left);
                String right           = this.Visit(e, e.Right);

                switch (e.Operator)
                {
                case ArithmeticOperator.Add:
                    return(String.Format("{0} add {1}", left, right));

                case ArithmeticOperator.Div:
                    return(String.Format("{0} div {1}", left, right));

                case ArithmeticOperator.Mod:
                    return(String.Format("{0} mod {1}", left, right));

                case ArithmeticOperator.Mult:
                    return(String.Format("{0} mul {1}", left, right));

                case ArithmeticOperator.Sub:
                    return(String.Format("{0} sub {1}", left, right));

                default:
                    throw new Exception("Unhandled Arithmetic Type: " + e.Operator);
                }
                ;
            }
            else if (node is MethodExpression)
            {
                MethodExpression e = (MethodExpression)node;
                return(BuildMemberOrMethodExpression(node, e.Caller, e.Arguments, e.Name));
            }
            else if (node is MemberExpression)
            {
                MemberExpression e = (MemberExpression)node;
                return(BuildMemberOrMethodExpression(node, e.Caller, e.Arguments, e.Name));
            }
            else if (node is ConstantExpression)
            {
                ConstantExpression e     = (ConstantExpression)node;
                object             value = e.Value.ClrValue;

                string val = TypeData.FormatForKey(value, this.UseSmallCasing, false);
                if (this.EscapeUriValues)
                {
                    // FormatForKey already does this for doubles that don't have the D
                    if (!(value is double) || Versioning.Server.SupportsV2Features)
                    {
                        val = Uri.EscapeDataString(val);
                    }
                }

                if (value == null)
                {
                    val = "null";
                }
                else if (!(value is String))
                {
                    val = val.Replace("+", "").Replace("%2B", "");
                }

                return(val);
            }
            else if (node is NegateExpression)
            {
                NegateExpression e = (NegateExpression)node;
                return("-(" + this.Visit(e, e.Argument) + ")");
            }
            else if (node is FirstExpression)
            {
                FirstExpression first = node as FirstExpression;
                return(this.Visit(first, first.Input));
            }
            else if (node is FirstOrDefaultExpression)
            {
                FirstOrDefaultExpression first = node as FirstOrDefaultExpression;
                return(this.Visit(first, first.Input));
            }
            else if (node is SingleExpression)
            {
                SingleExpression first = node as SingleExpression;
                return(this.Visit(first, first.Input));
            }
            else if (node is SingleOrDefaultExpression)
            {
                SingleOrDefaultExpression first = node as SingleOrDefaultExpression;
                return(this.Visit(first, first.Input));
            }
            else
            {
                throw new Exception(this.GetType().Name + " Unhandled Node: " + node.GetType().Name);
            }
        }
Beispiel #2
0
        private string Visit(Node caller, Node node)
        {
            if (node is ProjectExpression)
            {
                ProjectExpression e = (ProjectExpression)node;
                if (e.Projections.Count == 0)
                {
                    return(String.Format("<ProjectExpression><Input>{0}</Input></ProjectExpression>", this.Visit(e, e.Input)));
                }
                else if (e.Projections.Count == 1)
                {
                    //if (e.Projections[0] is PropertyExpression)
                    return(String.Format("<ProjectExpression><Input>{0}</Input><ProjectionZero>{1}</ProjectionZero></ProjectExpression>", this.Visit(e, e.Input), this.Visit(e, e.Projections[0])));
                    //else if (e.Projections[0] is ConstantExpression)
                    //    return String.Format("<ProjectExpression><Input>{0}</Input>{1}</ProjectExpression>", this.Visit(e, e.Input), this.Visit(e, e.Projections[0]));
                    //else return String.Format("encountered {0}", e.Projections[0].Name);
                }
                else
                {
                    throw new Exception("More than 1 projection, invalid");
                }
            }
            else if (node is ComparisonExpression)
            {
                return(WriteBinaryExpressionXml("ComparisonExpression", node));
            }
            else if (node is ArithmeticExpression)
            {
                return(WriteBinaryExpressionXml("ArithmeticException", node));
            }
            else if (node is ScanExpression)
            {
                ScanExpression e = (ScanExpression)node;
                if (e.Input is VariableExpression && ((VariableExpression)e.Input).Variable is ResourceContainer)
                {
                    return(String.Format("<ScanExpression><Input>{0}</Input></ScanExpression>", this.Visit(e, e.Input)));
                }

                throw new Exception("Unsupported on in scan expression");
            }
            else if (node is PredicateExpression)
            {
                PredicateExpression e = (PredicateExpression)node;
                return(String.Format("<PredicateExpression><Input>{0}</Input><Predicate>{1}</Predicate></PredicateExpression>",
                                     this.Visit(e, e.Input), this.Visit(e, e.Predicate)));
            }
            else if (node is NavigationExpression)
            {
                NavigationExpression e = (NavigationExpression)node;
                return(String.Format("<NavigationExpression IsLink=\"{2}\"><Input>{0}</Input><PropertyExp>{1}</PropertyExp></NavigationExpression>", this.Visit(e, e.Input), this.Visit(e, e.PropertyExp), e.IsLink.ToString()));
            }
            else if (node is KeyExpression)
            {
                string keyExpressionString = BuildKeyExpressionXml((KeyExpression)node);
                return(String.Format("<KeyExpression>{0}</KeyExpression>", keyExpressionString));
                //UriQueryBuilder.CreateKeyString((KeyExpression)node, false));
            }
            else if (node is NestedPropertyExpression)
            {
                NestedPropertyExpression e = (NestedPropertyExpression)node;

                string nestedProperty = "";
                foreach (PropertyExpression p in e.PropertyExpressions)
                {
                    nestedProperty += this.Visit(e, p);
                }

                return(String.Format("<NestedPropertyExpression>{0}</NestedPropertyExpression>", nestedProperty));
            }
            else if (node is PropertyExpression)
            {
                PropertyExpression e = (PropertyExpression)node;
                return(String.Format("<PropertyExpression Name=\"{0}\" ValueOnly=\"{1}\" />", e.Property.Name, e.ValueOnly.ToString()));
            }
            else if (node is VariableExpression)
            {
                VariableExpression e         = (VariableExpression)node;
                ResourceContainer  container = e.Variable as ResourceContainer;
                return(String.Format("<VariableExpression Name=\"{0}\" Type=\"{1}\"/>", container.Name,
                                     container.BaseType.ClrType != null ? container.BaseType.ClrType.FullName : container.BaseType.Name
                                     ));
            }
            else if (node is ConstantExpression)
            {
                ConstantExpression e = (ConstantExpression)node;
                return(String.Format("<ConstantExpression Value=\"{0}\" Type=\"{1}\" />", e.Value.ClrValue, e.Value.ClrValue.GetType().FullName));
            }
            else if (node is ServiceContainer)
            {
                string containerString = null;

                ServiceContainer e = (ServiceContainer)node;
                foreach (ResourceContainer container in e.ResourceContainers)
                {
                    containerString += this.Visit(e, container);
                }

                return(String.Format("<ServiceContainer Name=\"{0}\" Namespace=\"{1}\">{2}</ServiceContainer>", e.Name, e.Namespace, containerString));
            }
            else if (node is ResourceContainer)
            {
                string typeString = null;

                ResourceContainer e = (ResourceContainer)node;
                foreach (ResourceType type in e.ResourceTypes)
                {
                    typeString += this.Visit(e, type);
                }

                return(String.Format("<ResourceContainer Name=\"{0}\" Namespace=\"{1}\">{2}</ResourceContainer>", e.Name, e.Namespace, typeString));
            }
            else if (node is ResourceType)
            {
                string propertyString = null;

                ResourceType e = (ResourceType)node;
                foreach (ResourceProperty property in e.Properties)
                {
                    propertyString += this.Visit(e, property);
                }

                return(String.Format("<ResourceType Name=\"{0}\" Namespace=\"{1}\">{2}</ResourceType>", e.Name, e.Namespace, propertyString));
            }
            else if (node is ResourceProperty)
            {
                ResourceProperty e = (ResourceProperty)node;
                return(String.Format("<ResourceProperty Name=\"{0}\" IsNavigation=\"{1}\" IsComplexType=\"{2}\" />", e.Name, e.IsNavigation, e.IsComplexType));
            }
            else
            {
                return(String.Format("<{0} name=\'{1}\'></{0}>", node.GetType().Name, node.Name));
            }
        }