public static NamedExpression CreateFromMemberName(string memberName, Expression innerExpression)
        {
            LinqUtility.CheckNotNull("memberName", memberName);
            LinqUtility.CheckNotNull("innerExpression", innerExpression);

            return(new NamedExpression(memberName, innerExpression));
        }
        public NamedExpression(string name, Expression expression)
        {
            LinqUtility.CheckNotNull("expression", expression);

            Name       = name;
            Expression = expression;
        }
Esempio n. 3
0
        private void CheckQueryableType(Type queryableType)
        {
            LinqUtility.CheckTypeIsAssignableFrom("queryableType", queryableType, typeof(IQueryable));

            var queryableTypeInfo = queryableType.GetTypeInfo();

            if (!queryableTypeInfo.IsGenericTypeDefinition)
            {
                var message = string.Format(
                    "Expected the generic type definition of an implementation of IQueryable<T>, but was '{0}'.",
                    queryableType);
                throw new ArgumentException(message, "queryableType");
            }

            var genericArgumentCount = queryableTypeInfo.GenericTypeParameters.Length;

            if (genericArgumentCount != 1)
            {
                var message = string.Format(
                    "Expected the generic type definition of an implementation of IQueryable<T> with exactly one type argument, but found {0} arguments on '{1}.",
                    genericArgumentCount,
                    queryableType);
                throw new ArgumentException(message, "queryableType");
            }
        }
Esempio n. 4
0
        protected override Expression VisitNew(NewExpression expression)
        {
            // Select(g=>g)
            if (expression.Type.Name == "Grouping`2")
            {
                var groupByClause = LinqUtility.PriorGroupBy(ModelVisitor)[0];
                ModelVisitor.QueryText.AppendFormat(" {0} ",
                                                    LinqUtility.ResolvePropertyName(groupByClause.TranslateIntoName()));
                return(expression);
            }

            if (!TreatNewWithoutBracket)
            {
                ModelVisitor.QueryText.Append(" { ");
            }
            var e = (NewExpression)NamedExpression.CreateNewExpressionWithNamedArguments(ModelVisitor.Db, expression);

            for (var i = 0; i < e.Arguments.Count; i++)
            {
                Visit(e.Arguments[i]);
                if (i != e.Arguments.Count - 1)
                {
                    ModelVisitor.QueryText.Append(" , ");
                }
            }

            if (!TreatNewWithoutBracket)
            {
                ModelVisitor.QueryText.Append(" } ");
            }

            return(e);
        }
Esempio n. 5
0
        /// <summary>
        ///     Initializes a new instance of <see cref="ArangoQueryProvider" /> using a custom <see cref="IQueryParser" />.
        /// </summary>
        public ArangoQueryProvider(Type queryableType, IQueryParser queryParser, IQueryExecutor executor,
                                   IArangoLinq db)
            : base(LinqUtility.CheckNotNull("queryParser", queryParser), LinqUtility.CheckNotNull("executor", executor))
        {
            LinqUtility.CheckNotNull("queryableType", queryableType);
            CheckQueryableType(queryableType);

            QueryableType = queryableType;
            this.db       = db;
        }
        protected override Expression Accept(ExpressionVisitor visitor)
        {
            LinqUtility.CheckNotNull("visitor", visitor);

            var specificVisitor = visitor as INamedExpressionVisitor;

            if (specificVisitor != null)
            {
                return(specificVisitor.VisitNamed(this));
            }
            return(base.Accept(visitor));
        }
        protected override Expression VisitChildren(ExpressionVisitor visitor)
        {
            LinqUtility.CheckNotNull("visitor", visitor);

            var newExpression = visitor.Visit(Expression);

            if (newExpression != Expression)
            {
                return(new NamedExpression(Name, newExpression));
            }
            return(this);
        }
Esempio n. 8
0
        protected override Expression VisitConstant(ConstantExpression expression)
        {
            var parentModelVisitor = LinqUtility.FindParentModelVisitor(ModelVisitor);

            parentModelVisitor.ParameterNameCounter++;
            var parameterName = "P" + parentModelVisitor.ParameterNameCounter;

            parentModelVisitor.QueryData.BindVars.Add(parameterName, expression.Value);

            ModelVisitor.QueryText.AppendFormat(" @{0} ", parameterName);

            return(expression);
        }
Esempio n. 9
0
        public QueryData GetQueryData(Expression expression)
        {
            LinqUtility.CheckNotNull("expression", expression);

            var queryModel = GenerateQueryModel(expression);

            var visitor = new ArangoModelVisitor(db);

            visitor.VisitQueryModel(queryModel);

            visitor.QueryData.Query = visitor.QueryText.ToString();

            return(visitor.QueryData);
        }
        //public static Expression WrapIntoNamedExpression(string memberName, Expression argumentExpression)
        //{
        //    var expressionAsNamedExpression = argumentExpression as NamedExpression;
        //    if (expressionAsNamedExpression != null && expressionAsNamedExpression.Name == memberName)
        //        return expressionAsNamedExpression;

        //    return CreateFromMemberName(memberName, argumentExpression);
        //}

        public static Expression WrapIntoNamedExpression(IArangoLinq db, MemberInfo memberInfo,
                                                         Expression argumentExpression)
        {
            var memberName = LinqUtility.ResolveMemberNameRaw(db, memberInfo);

            var expressionAsNamedExpression = argumentExpression as NamedExpression;

            if (expressionAsNamedExpression != null && expressionAsNamedExpression.Name == memberName)
            {
                return(expressionAsNamedExpression);
            }

            var namedExpression = CreateFromMemberName(memberName, argumentExpression);

            return(namedExpression);
        }
Esempio n. 11
0
        protected internal override Expression VisitQuerySourceReference(QuerySourceReferenceExpression expression)
        {
            ModelVisitor.QueryText.AppendFormat(" {0}",
                                                LinqUtility.ResolvePropertyName(expression.ReferencedQuerySource.ItemName));

            var mainFromClause = expression.ReferencedQuerySource as MainFromClause;

            //  .Select(g => g.Select(gList => gList.Age)) subquery select, handle prior groupby source parameter names
            if (mainFromClause != null && mainFromClause.FromExpression.Type.Name == "IGrouping`2")
            {
                var groupByClauses = LinqUtility.PriorGroupBy(ModelVisitor);

                for (var i = 0; i < groupByClauses.Count; i++)
                {
                    ModelVisitor.QueryText.AppendFormat("{0}{1}{2}"
                                                        , i == 0 ? "." : ""
                                                        , LinqUtility.ResolvePropertyName(groupByClauses[i].FromParameterName)
                                                        , i != groupByClauses.Count - 1 ? "." : "");
                }
            }

            return(expression);
        }
Esempio n. 12
0
        protected override Expression VisitParameter(ParameterExpression expression)
        {
            var name = expression.Name;

            if (expression.Type.Name == "TraversalData`2")
            {
                var prefix = LinqUtility.MemberNameFromMap(name, "graph", ModelVisitor);

                ModelVisitor.QueryText.AppendFormat(" {{ {0} : {1}, {2} : {3}, {4} : {5} }} ",
                                                    LinqUtility.ResolvePropertyName("vertex"),
                                                    LinqUtility.ResolvePropertyName($"{prefix}_Vertex"),
                                                    LinqUtility.ResolvePropertyName("edge"),
                                                    LinqUtility.ResolvePropertyName($"{prefix}_Edge"),
                                                    LinqUtility.ResolvePropertyName("path"),
                                                    LinqUtility.ResolvePropertyName($"{prefix}_Path"));

                return(expression);
            }

            if (expression.Type.Name == "ShortestPathData`2")
            {
                var prefix = LinqUtility.MemberNameFromMap(name, "graph", ModelVisitor);

                ModelVisitor.QueryText.AppendFormat(" {{ {0} : {1}, {2} : {3} }} ",
                                                    LinqUtility.ResolvePropertyName("vertex"),
                                                    LinqUtility.ResolvePropertyName($"{prefix}_Vertex"),
                                                    LinqUtility.ResolvePropertyName("edge"),
                                                    LinqUtility.ResolvePropertyName($"{prefix}_Edge"));

                return(expression);
            }

            ModelVisitor.QueryText.AppendFormat(" {0} ", LinqUtility.ResolvePropertyName(name));

            return(expression);
        }
        public static Expression CreateNewExpressionWithNamedArguments(IArangoLinq db, NewExpression expression)
        {
            LinqUtility.CheckNotNull("expression", expression);

            return(CreateNewExpressionWithNamedArguments(db, expression, expression.Arguments));
        }
Esempio n. 14
0
        protected override Expression VisitMethodCall(MethodCallExpression expression)
        {
            if (expression.Method.Name == "As" && expression.Method.DeclaringType == typeof(Aql))
            {
                Visit(expression.Arguments[0]);
                return(expression);
            }

            if (expression.Method.Name == "get_Item" && expression.Method.DeclaringType.Name == "IList`1")
            {
                Visit(expression.Object);
                ModelVisitor.QueryText.Append(" [ ");
                Visit(expression.Arguments[0]);
                ModelVisitor.QueryText.Append(" ] ");
                return(expression);
            }

            string methodName = expression.Method.Name;
            bool   prop       = false;

            if (expression.Method.DeclaringType == typeof(Math))
            {
                if (MathMethods.TryGetValue(expression.Method.Name, out var m))
                {
                    methodName = m;
                }
                else
                {
                    throw new InvalidOperationException($"Method {expression.Method.Name} is not supported in ArangoLinqProvider");
                }
            }
            else
            {
                var aqlFunction = expression.Method.GetCustomAttribute <AqlFunctionAttribute>();

                if (aqlFunction == null)
                {
                    throw new InvalidOperationException($"Method {expression.Method.Name} is not supported in ArangoLinqProvider");
                }

                methodName = aqlFunction.Name;
                prop       = aqlFunction.IsProperty;
            }

            string argumentSeprator = null;
            var    noParenthesis    = MethodsWithNoParenthesis.TryGetValue(methodName, out argumentSeprator) || prop;

            if (!noParenthesis)
            {
                ModelVisitor.QueryText.AppendFormat(" {0}( ", methodName);
                argumentSeprator = " , ";
            }

            Type[] genericArguments = null;
            if (MethodsWithFirstGenericArgument.Contains(methodName))
            {
                genericArguments = expression.Method.GetGenericArguments();
                var collection = LinqUtility.ResolveCollectionName(ModelVisitor.Db, genericArguments[0]);
                ModelVisitor.QueryText.AppendFormat(" {0}{1}", collection, argumentSeprator);
            }

            if (MethodsWithSecondGenericArgument.Contains(methodName))
            {
                var collection = LinqUtility.ResolveCollectionName(ModelVisitor.Db, genericArguments[1]);
                ModelVisitor.QueryText.AppendFormat(" {0}{1}", collection, argumentSeprator);
            }

            for (var i = 0; i < expression.Arguments.Count; i++)
            {
                Visit(expression.Arguments[i]);

                if (i != expression.Arguments.Count - 1)
                {
                    ModelVisitor.QueryText.Append(argumentSeprator);
                }
            }

            if (!noParenthesis)
            {
                ModelVisitor.QueryText.Append(" ) ");
            }

            return(expression);
        }
Esempio n. 15
0
        protected override Expression VisitMember(MemberExpression expression)
        {
            var member = expression.Expression as MemberExpression;

            if (member != null && member.Expression.Type.Name == "IGrouping`2")
            {
                ModelVisitor.QueryText.AppendFormat(" {0} ", LinqUtility.ResolvePropertyName(expression.Member.Name));
            }
            // Select(g=>g.Key)
            else if (expression.Expression.Type.Name == "IGrouping`2")
            {
                var groupByClause = LinqUtility.PriorGroupBy(ModelVisitor)[0];

                var newExpression = groupByClause.Selector as NewExpression;
                if (newExpression != null)
                {
                    ModelVisitor.QueryText.Append(" { ");
                    for (var i = 0; i < newExpression.Members.Count; i++)
                    {
                        var memberName = newExpression.Members[i].Name;
                        ModelVisitor.QueryText.AppendFormat(" {0} : {1} ",
                                                            LinqUtility.ResolvePropertyName(memberName),
                                                            LinqUtility.ResolvePropertyName(memberName));

                        if (i != newExpression.Members.Count - 1)
                        {
                            ModelVisitor.QueryText.Append(" , ");
                        }
                    }

                    ModelVisitor.QueryText.Append(" } ");
                }

                if (groupByClause.Selector.NodeType != ExpressionType.New)
                {
                    ModelVisitor.QueryText.AppendFormat(" {0} ",
                                                        LinqUtility.ResolvePropertyName(groupByClause.CollectVariableName));
                }
            }
            else if (expression.Expression.Type.Name == "TraversalData`2" ||
                     expression.Expression.Type.Name == "ShortestPathData`2")
            {
                var parameterExpression = expression.Expression as ParameterExpression;
                if (parameterExpression == null)
                {
                    throw new InvalidOperationException(
                              "[TraversalData`2|ShortestPathData`2] VisitMember, expected a ParameterExpression");
                }

                var prefix = LinqUtility.MemberNameFromMap(parameterExpression.Name, "graph", ModelVisitor);

                ModelVisitor.QueryText.AppendFormat(
                    LinqUtility.ResolvePropertyName($"{prefix}_{expression.Member.Name}"));
            }
            else
            {
                Visit(expression.Expression);
                ModelVisitor.QueryText.AppendFormat(".{0} ",
                                                    LinqUtility.ResolveMemberName(ModelVisitor.Db, expression.Member));
            }

            return(expression);
        }