public NamedExpression(string name, Expression expression)
        {
            LinqUtility.CheckNotNull("expression", expression);

            _name       = name;
            _expression = expression;
        }
Exemple #2
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 (int 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);
        }
        public static NamedExpression CreateFromMemberName(string memberName, Expression innerExpression)
        {
            LinqUtility.CheckNotNull("memberName", memberName);
            LinqUtility.CheckNotNull("innerExpression", innerExpression);

            return(new NamedExpression(memberName, innerExpression));
        }
Exemple #4
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 (int i = 0; i < newExpression.Members.Count; i++)
                    {
                        string 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");
                }

                string 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);
        }
        /// <summary>
        /// Initializes a new instance of <see cref="ArangoQueryProvider"/> using a custom <see cref="IQueryParser"/>.
        /// </summary>
        public ArangoQueryProvider(Type queryableType, IQueryParser queryParser, IQueryExecutor executor, IArangoDatabase db)
            : base(LinqUtility.CheckNotNull("queryParser", queryParser), LinqUtility.CheckNotNull("executor", executor))
        {
            LinqUtility.CheckNotNull("queryableType", queryableType);
            CheckQueryableType(queryableType);

            _queryableType = queryableType;
            this.db        = db;
        }
Exemple #6
0
        public static IEnumerable <MethodInfo> WhereNameMatches(this IEnumerable <MethodInfo> input, string name)
        {
            LinqUtility.CheckNotNull("input", input);
            LinqUtility.CheckNotNullOrEmpty("name", name);

            if (input.Count(mi => mi.Name == name) == 0)
            {
                throw new InvalidOperationException($"No suitable queryable method found for {name}");
            }

            return(input.Where(mi => mi.Name == name));
        }
Exemple #7
0
        public void VisitUpsertClause(UpsertClause upsertClause, QueryModel queryModel)
        {
            QueryText.Append(" upsert ");
            GetAqlExpression(upsertClause.SearchSelector, queryModel);

            QueryText.Append(" insert ");
            GetAqlExpression(upsertClause.InsertSelector, queryModel);

            QueryText.Append(" update ");
            GetAqlExpression(upsertClause.UpdateSelector, queryModel);

            QueryText.AppendFormat(" in {0} ", LinqUtility.ResolveCollectionName(Db, upsertClause.CollectionType));
        }
Exemple #8
0
        public void VisitLetClause(LetClause letClause, QueryModel queryModel, Type lhsType)
        {
            QueryText.AppendFormat(" let {0} = ", LinqUtility.ResolvePropertyName(letClause.ItemName));
            GetAqlExpression(letClause.LetExpression, queryModel);

            var subQuery = letClause.SubqueryExpression as SubQueryExpression;

            if (subQuery != null)
            {
                GetAqlExpression(subQuery, queryModel, handleLet: true);
                this.DontReturn = true;
            }
        }
        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);
        }
Exemple #10
0
        public void VisitRemoveClause(RemoveClause removeClause, QueryModel queryModel)
        {
            if (removeClause.KeySelector != null)
            {
                QueryText.Append(" remove ");

                GetAqlExpression(removeClause.KeySelector, queryModel);
            }
            else
            {
                QueryText.AppendFormat(" remove {0} ", LinqUtility.ResolvePropertyName(removeClause.ItemName));
            }

            QueryText.AppendFormat(" in {0} ", LinqUtility.ResolveCollectionName(Db, removeClause.CollectionType));
        }
Exemple #11
0
        public void VisitInsertClause(InsertClause insertClause, QueryModel queryModel)
        {
            if (insertClause.WithSelector != null)
            {
                QueryText.Append(" insert ");

                GetAqlExpression(insertClause.WithSelector, queryModel);
            }
            else
            {
                QueryText.AppendFormat(" insert {0} ", LinqUtility.ResolvePropertyName(insertClause.ItemName));
            }

            QueryText.AppendFormat(" in {0} ", LinqUtility.ResolveCollectionName(Db, insertClause.CollectionType));
        }
        //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(IArangoDatabase 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);
        }
        protected override Expression VisitChildren(ExpressionVisitor visitor)
        {
            LinqUtility.CheckNotNull("visitor", visitor);

            var newExpression = visitor.Visit(_expression);

            if (newExpression != _expression)
            {
                return(new NamedExpression(_name, newExpression));
            }
            else
            {
                return(this);
            }
        }
        protected override Expression Accept(ExpressionVisitor visitor)
        {
            LinqUtility.CheckNotNull("visitor", visitor);

            var specificVisitor = visitor as INamedExpressionVisitor;

            if (specificVisitor != null)
            {
                return(specificVisitor.VisitNamed(this));
            }
            else
            {
                return(base.Accept(visitor));
            }
        }
Exemple #15
0
        protected override Expression VisitConstant(ConstantExpression expression)
        {
            var parentModelVisitor = LinqUtility.FindParentModelVisitor(this.ModelVisitor);

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

            parentModelVisitor.QueryData.BindVars.Add(new QueryParameter()
            {
                Name = parameterName, Value = expression.Value
            });

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

            return(expression);
        }
Exemple #16
0
        public override void VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
        {
            if (fromClause.FromExpression as SubQueryExpression != null)
            {
                throw new Exception("MainFromClause.FromExpression cant be SubQueryExpression because group by is handle in itself clause");
            }

            string fromName = LinqUtility.ResolveCollectionName(this.Db, fromClause.ItemType);

            //  .Select(g => g.Select(gList => gList.Age)) subquery select, changes the fromName to group name (C1 for example)
            if (fromClause.FromExpression.Type.Name == "IGrouping`2")
            {
                var groupByClause = LinqUtility.PriorGroupBy(ParnetModelVisitor);

                var parentMVisitor = LinqUtility.FindParentModelVisitor(this);
                //parentMVisitor.GroupByNameCounter++;

                fromName = groupByClause[0].TranslateIntoName();

                fromName = LinqUtility.ResolvePropertyName(fromName);
            }

            //  == "IGrouping`2" => .Select(g => g.Select(gList => gList.Age)) subquery select
            if (fromClause.FromExpression.NodeType == ExpressionType.Constant ||
                fromClause.FromExpression.Type.Name == "IGrouping`2")
            {
                if (fromClause.FromExpression.Type.Name == "ArangoQueryable`1" || fromClause.FromExpression.Type.Name == "IGrouping`2")
                {
                    QueryText.AppendFormat(" for {0} in {1} ", LinqUtility.ResolvePropertyName(fromClause.ItemName), fromName);
                }
                else
                {
                    QueryText.AppendFormat(" for {0} in ", LinqUtility.ResolvePropertyName(fromClause.ItemName));
                    GetAqlExpression(fromClause.FromExpression, queryModel);
                }
            }
            else
            {
                QueryText.AppendFormat(" for {0} in ", LinqUtility.ResolvePropertyName(fromClause.ItemName));
                GetAqlExpression(fromClause.FromExpression, queryModel);
            }

            base.VisitMainFromClause(fromClause, queryModel);
        }
Exemple #17
0
        public void VisitUpdateReplaceClause(UpdateReplaceClause updateReplaceClause, QueryModel queryModel)
        {
            if (updateReplaceClause.KeySelector != null)
            {
                QueryText.AppendFormat(" {0} ", updateReplaceClause.Command);

                GetAqlExpression(updateReplaceClause.KeySelector, queryModel);

                QueryText.AppendFormat(" with ");
            }
            else
            {
                QueryText.AppendFormat(" {0} {1} with ", updateReplaceClause.Command, LinqUtility.ResolvePropertyName(updateReplaceClause.ItemName));
            }

            GetAqlExpression(updateReplaceClause.WithSelector, queryModel);

            QueryText.AppendFormat(" in {0} ", LinqUtility.ResolveCollectionName(Db, updateReplaceClause.CollectionType));
        }
Exemple #18
0
        public override void VisitAdditionalFromClause(AdditionalFromClause fromClause, QueryModel queryModel, int index)
        {
            var subQuery = fromClause.FromExpression as SubQueryExpression;

            if (subQuery != null)
            {
                this.DontReturn = true;
                GetAqlExpression(subQuery, queryModel, handleJoin: true);
            }
            else if (fromClause.FromExpression.Type.Name == "ArangoQueryable`1")
            {
                string fromName = LinqUtility.ResolveCollectionName(this.Db, fromClause.ItemType);
                QueryText.AppendFormat(" for {0} in {1} ", LinqUtility.ResolvePropertyName(fromClause.ItemName), fromName);
            }
            else
            {
                QueryText.AppendFormat(" for {0} in ", LinqUtility.ResolvePropertyName(fromClause.ItemName));
                GetAqlExpression(fromClause.FromExpression, queryModel);
            }
        }
Exemple #19
0
        protected 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 (int 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);
        }
Exemple #20
0
        public void VisitGroupByClause(GroupByClause groupByClause, QueryModel queryModel, int index)
        {
            var parentMVisitor = LinqUtility.FindParentModelVisitor(this);

            parentMVisitor.GroupByNameCounter++;
            groupByClause.IntoName          = "C" + parentMVisitor.GroupByNameCounter;
            groupByClause.FuncIntoName      = Db.Setting.Linq.TranslateGroupByIntoName;
            groupByClause.FromParameterName = queryModel.MainFromClause.ItemName;

            QueryText.Append(" collect ");

            if (groupByClause.Selector.NodeType != ExpressionType.New)
            {
                groupByClause.CollectVariableName = "CV" + parentMVisitor.GroupByNameCounter;
                QueryText.AppendFormat(" {0} = ", LinqUtility.ResolvePropertyName(groupByClause.CollectVariableName));
            }

            GetAqlExpression(groupByClause.Selector, queryModel, true);

            QueryText.AppendFormat(" into {0} ", LinqUtility.ResolvePropertyName(groupByClause.TranslateIntoName()));

            groupByClause.Visited = true;
        }
        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");
            }
        }
Exemple #22
0
        protected override Expression VisitParameter(ParameterExpression expression)
        {
            string name = expression.Name;

            if (expression.Type.Name == "TraversalData`2")
            {
                string 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")
            {
                string 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(IArangoDatabase db, NewExpression expression)
        {
            LinqUtility.CheckNotNull("expression", expression);

            return(CreateNewExpressionWithNamedArguments(db, expression, expression.Arguments));
        }
Exemple #24
0
        public static IEnumerable <MethodInfo> WithoutResultSelector(this IEnumerable <MethodInfo> input)
        {
            LinqUtility.CheckNotNull("input", input);

            return(input.Where(mi => mi.GetParameters().All(p => p.Name != "resultSelector")));
        }
Exemple #25
0
        public static IEnumerable <MethodInfo> WithoutEqualityComparer(this IEnumerable <MethodInfo> input)
        {
            LinqUtility.CheckNotNull("input", input);

            return(input.Where(mi => !HasGenericDelegateOfType(mi, typeof(IEqualityComparer <>))));
        }
Exemple #26
0
        protected override Expression VisitMethodCall(MethodCallExpression expression)
        {
            if (expression.Method.Name == "As")
            {
                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;
            var    userFunction = ModelVisitor.Db.SharedSetting.AqlFunctions.FindFunctionAttribute(expression.Method);
            bool   methodExists = userFunction != null;

            if (methodExists)
            {
                methodName = userFunction.Name;
            }
            else
            {
                methodExists = aqlMethods.TryGetValue(expression.Method.Name, out methodName);
            }

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

            string argumentSeprator = null;
            bool   noParenthesis    = methodsWithNoParenthesis.TryGetValue(methodName, out argumentSeprator);

            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 (int 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);
        }
Exemple #27
0
        public void VisitTraversalClause(ITraversalClause traversalClause, QueryModel queryModel, int index)
        {
            string prefix = LinqUtility.MemberNameFromMap(traversalClause.Identifier, "graph", this);

            if (traversalClause.TargetVertex != null)
            {
                QueryText.AppendFormat(" for {0}, {1} in ",
                                       LinqUtility.ResolvePropertyName($"{prefix}_Vertex"),
                                       LinqUtility.ResolvePropertyName($"{prefix}_Edge"));
            }
            else
            {
                QueryText.AppendFormat(" for {0}, {1}, {2} in ",
                                       LinqUtility.ResolvePropertyName($"{prefix}_Vertex"),
                                       LinqUtility.ResolvePropertyName($"{prefix}_Edge"),
                                       LinqUtility.ResolvePropertyName($"{prefix}_Path"));
            }

            if (traversalClause.Min != null && traversalClause.Max != null)
            {
                QueryText.AppendFormat(" {0}..{1} ", traversalClause.Min.Value, traversalClause.Max);
            }

            QueryText.AppendFormat(" {0} ", traversalClause.Direction != null
                ? traversalClause.Direction.Value
                : Utils.EdgeDirectionToString(EdgeDirection.Any));

            if (traversalClause.TargetVertex != null)
            {
                QueryText.Append(" shortest_path ");
                GetAqlExpression(traversalClause.StartVertex, queryModel);
                QueryText.Append(" to ");
                GetAqlExpression(traversalClause.TargetVertex, queryModel);
            }
            else
            {
                GetAqlExpression(traversalClause.StartVertex, queryModel);
            }

            if (string.IsNullOrEmpty(traversalClause.GraphName) == false)
            {
                QueryText.AppendFormat("  graph \"{0}\" ", traversalClause.GraphName);
            }
            else
            {
                StringBuilder edges = new StringBuilder();

                for (int i = 0; i < traversalClause.EdgeCollections.Count; i++)
                {
                    var e = traversalClause.EdgeCollections[i];

                    if (i != 0)
                    {
                        edges.Append(", ");
                    }

                    if (e.Direction.HasValue)
                    {
                        edges.AppendFormat("{0} ", Utils.EdgeDirectionToString(e.Direction.Value));
                    }

                    edges.Append(LinqUtility.ResolvePropertyName(e.CollectionName));
                }

                QueryText.Append(edges);
            }

            if (traversalClause.Options != null)
            {
                QueryText.AppendFormat(" options {0} ", new DocumentSerializer(Db).SerializeWithoutReader(traversalClause.Options.Value));
            }
        }
Exemple #28
0
        public static IEnumerable <MethodInfo> WithoutIndexSelector(this IEnumerable <MethodInfo> input, int parameterPosition)
        {
            LinqUtility.CheckNotNull("input", input);

            return(input.Where(mi => !HasIndexSelectorParameter(mi, parameterPosition)));
        }