예제 #1
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);
        }