コード例 #1
0
 public abstract void Visit(SelectClause expression);
コード例 #2
0
ファイル: QueryTranslator.cs プロジェクト: npenin/uss
        public NLinq.Expressions.Expression Visit(MethodCallExpression call)
        {
            if (call.Method.IsStatic && (call.Method.DeclaringType == typeof(Enumerable) || call.Method.DeclaringType == typeof(Queryable)) || call.Method.DeclaringType == typeof(ContextExtensions))
            {
                NLinq.Expressions.Expression subExpression = Visit(call.Arguments[0]);
                switch (call.Method.Name)
                {
                    case "Skip":
                    case "Take":
                        if (tree == null)
                        {
                            if (subExpression == null)
                                subExpression = new QueryExpression(from, new QueryBody(clauses, new SelectClause(identifiers["#First"]), null));
                            tree = new NLINQ.MemberExpression(new MethodCall(call.Method.Name, Visit(call.Arguments[1])), subExpression);
                        }
                        else
                            tree = new NLINQ.MemberExpression(new MethodCall(call.Method.Name, Visit(call.Arguments[1])), tree);
                        return tree;
                    case "Cast":
                        return subExpression;
                    case "FirstOrDefault":
                    case "LastOrDefault":
                    case "Last":
                    case "First":
                        string methodName = call.Method.Name;
                        if (call.Method.Name.EndsWith("OrDefault"))
                            methodName = methodName.Substring(0, call.Method.Name.Length - 9);
                        if (tree == null)
                        {
                            if (subExpression == null)
                                subExpression = new QueryExpression(from, new QueryBody(clauses, new SelectClause(identifiers["#First"]), null));
                            tree = new NLINQ.MemberExpression(new MethodCall(new Identifier(methodName)), subExpression);
                        }
                        else
                            tree = new NLINQ.MemberExpression(new MethodCall(new Identifier(methodName)), tree);
                        break;
                    case "Select":
                        SelectOrGroupClause select = new SelectClause(Visit(call.Arguments[1]));
                        tree = new QueryExpression(from, new QueryBody(clauses, select, null));
                        identifiers["#First"].Text = "e" + identifiers["#First"].GetHashCode();
                        identifiers.Clear();
                        identifiers["#First"] = new Identifier("e0");
                        clauses = new ClauseList();
                        from = new FromClause(null, identifiers["#First"], tree);
                        return tree;
                    case "SelectMany":
                        //Select the result of the join (a load reference)
                        FromClause from2 = null;
                        Identifier newElement = null;
                        select = null;

                        if (call.Arguments.Count == 2)
                            newElement = new Identifier("e1");
                        if (call.Arguments.Count == 3)
                            newElement = new Identifier(((System.Linq.Expressions.LambdaExpression)((System.Linq.Expressions.UnaryExpression)call.Arguments[2]).Operand).Parameters[1].Name);

                        from2 = new FromClause(call.Method.GetGenericArguments()[1].FullName, newElement, Visit(call.Arguments[1]));

                        if (call.Arguments.Count == 2)
                        {
                            //System.Linq.Expressions.LambdaExpression lambda = ((System.Linq.Expressions.LambdaExpression)((System.Linq.Expressions.UnaryExpression)call.Arguments[1]).Operand);
                            identifiers["#First"].Text = "e" + identifiers["#First"].GetHashCode();

                            //newElement = new Identifier("e0");
                            identifiers.Clear();
                            newElement.Text = "e0";
                            identifiers.Add("#First", newElement);
                            identifiers.Add(newElement.Text, newElement);
                            select = new SelectClause(newElement);
                        }

                        //Select a property of the select many
                        if (call.Arguments.Count == 3)
                        {
                            //newElement = new Identifier(((System.Linq.Expressions.LambdaExpression)((System.Linq.Expressions.UnaryExpression)call.Arguments[2]).Operand).Parameters[1].Name);
                            identifiers.Add(newElement.Text, newElement);
                            Identifier src = new Identifier(((System.Linq.Expressions.LambdaExpression)((System.Linq.Expressions.UnaryExpression)call.Arguments[2]).Operand).Parameters[0].Name);
                            if (!identifiers.ContainsKey(src.Text))
                                identifiers.Add(src.Text, identifiers["#First"]);
                            select = new SelectClause(Visit(((System.Linq.Expressions.LambdaExpression)((System.Linq.Expressions.UnaryExpression)call.Arguments[2]).Operand).Body));
                            if (((SelectClause)select).Expression == null)
                                select = null;
                        }
                        if (call.Arguments.Count > 3)
                            throw new NotSupportedException();
                        //from2 = new FromClause(call.Method.GetGenericArguments()[1].FullName, newElement, Visit(((System.Linq.Expressions.LambdaExpression)((System.Linq.Expressions.UnaryExpression)call.Arguments[1]).Operand).Body));
                        clauses.Add(from2);
                        tree = new QueryExpression(from, new QueryBody(clauses, select, null));
                        break;
                    case "Avg":
                    case "Sum":
                        if (subExpression == null)
                            subExpression = new QueryExpression(from, new QueryBody(clauses, new SelectClause(identifiers["#First"]), null));
                        tree = new NLINQ.MemberExpression(new MethodCall(new Identifier(call.Method.Name)), subExpression);
                        return tree;
                    case "Count":
                    case "Any":
                        if (call.Arguments.Count > 1)
                        {
                            if (subExpression != null)
                            {
                                var param = ((System.Linq.Expressions.LambdaExpression)call.Arguments[1]).Parameters[0];
                                var paramIdent = new Identifier(param.Name);
                                identifiers.Add(param.Name, paramIdent);
                                subExpression = new QueryExpression(new FromClause(param.Type.FullName, paramIdent, subExpression), new QueryBody(new ClauseList(new List<QueryBodyClause>() { new WhereClause(Visit(call.Arguments[1])) }), new SelectClause(paramIdent), null));
                            }
                            else
                                clauses.Add(new WhereClause(Visit(call.Arguments[1])));
                        }
                        if (subExpression == null)
                            subExpression = new QueryExpression(from, new QueryBody(clauses, new SelectClause(identifiers["#First"]), null));
                        tree = new NLINQ.MemberExpression(new MethodCall(new Identifier(call.Method.Name)), subExpression);
                        return tree;
                    case "Intersect":
                        break;
                    case "Where":
                        clauses.Add(new WhereClause(Visit(call.Arguments[1])));
                        tree = null;
                        if (from == null)
                            from = new FromClause(null, identifiers["#First"], subExpression);
                        break;
                    case "OrderBy":
                        NLINQ.Expression m = Visit(call.Arguments[1]);
                        clauses.Add(new OrderByClause(new OrderByCriteria(m, true)));
                        break;
                    case "ThenBy":
                        break;
                    case "Infer":
                        var inferQuery = new QueryTranslator(from.Type);
                        var result = inferQuery.Visit(call.Arguments[1]);
                        var first = inferQuery.identifiers["#First"];
                        NLINQ.MemberExpression me = (NLINQ.MemberExpression)result;
                        while (me.Previous != first)
                            me = (NLINQ.MemberExpression)me.Previous;
                        me.Previous = null;
                        if (inferQuery.tree != null)
                            result = inferQuery.tree;
                        InferredRelationShips.Add(result.ToString());
                        tree = subExpression;
                        return subExpression;
                }
            }
            if (call.Method.DeclaringType == typeof(string) || call.Method.DeclaringType == typeof(DateTime))
            {
                NLinq.Expressions.Expression source = Visit(call.Object);
                List<NLinq.Expressions.Expression> parameters = new List<NLinq.Expressions.Expression>();
                foreach (var arg in call.Arguments)
                {
                    var param = Visit(arg);
                    if (param != null)
                        parameters.Add(param);
                }
                return new NLinq.Expressions.MemberExpression(
                    new NLinq.Expressions.MethodCall(new NLinq.Expressions.Identifier(call.Method.Name), parameters.ToArray()),
                    source);
            }
            return null;
        }
コード例 #3
0
ファイル: NLinqExpressionUpdater.cs プロジェクト: npenin/uss
 public SelectClause Update(SelectClause expression, Expression exp)
 {
     if (expression.Expression != exp)
         return new SelectClause(exp);
     return expression;
 }