Exemplo n.º 1
0
 public SelectStatement(TableAlias alias, IEnumerable<IAliasedExpression> columns, FromClause from, OrderByClause orderby, WhereClause where)
     : this(alias)
 {
     Columns = columns;
     From = from;
     OrderBy = orderby;
     Where = where;
 }
Exemplo n.º 2
0
        public QueryExpression(FromClause from, QueryBody queryBody)
        {
            if (from == null)
                throw new ArgumentNullException("from");

            if (queryBody == null)
                throw new ArgumentNullException("queryBody");

            this.from = from;
            this.queryBody = queryBody;
        }
Exemplo n.º 3
0
 public override QueryBodyClause Visit(FromClause expression)
 {
     if (expression.Expression == null || (expression.Expression is Identifier && ((Identifier)expression.Expression).Text == defaultSource))
     {
         for (int i = Entities.Count - 1; i >= 0; i--)
         {
             if (!model.Inherits(Entities[i].Type, expression.Type))
                 Entities.RemoveAt(i);
         }
     }
     return base.Visit(expression);
 }
Exemplo n.º 4
0
 public override QueryBodyClause Visit(FromClause expression)
 {
     if (!inQueryBody)
     {
         Visit(expression.Expression);
         identifiers.Add(expression.Identifier.Text, expression.Identifier);
     }
     else
     {
         var oldResult = result;
         Visit(expression.Expression);
         identifiers.Add(expression.Identifier.Text, expression.Identifier);
         result = Linq.Expression.Call(null, SelectMany, oldResult, Linq.Expression.Quote(result));
     }
     return expression;
 }
Exemplo n.º 5
0
 public DeleteStatement(FromClause from, WhereClause where)
 {
     From = from;
     Where = where;
 }
Exemplo n.º 6
0
 public virtual IAliasedExpression Update(SelectStatement item, IEnumerable<IAliasedExpression> columns, FromClause from, Evaluant.NLinq.Expressions.WhereClause where, Evaluant.NLinq.Expressions.OrderByClause orderby, TableAlias alias)
 {
     if (item.Alias != alias || item.Columns != columns || item.From != from || item.OrderBy != orderby || item.Where != where)
         return new SelectStatement(alias, columns, from, orderby, where) { Top = item.Top, Distinct = item.Distinct };
     return item;
 }
Exemplo n.º 7
0
 public virtual IDbStatement Update(DeleteStatement item, FromClause from, Evaluant.NLinq.Expressions.WhereClause where)
 {
     if (item.Where != where)
         return new DeleteStatement(from, where);
     return item;
 }
Exemplo n.º 8
0
 public virtual FromClause Update(FromClause item, IAliasedExpression[] originalFroms, IEnumerable<IAliasedExpression> froms)
 {
     if (originalFroms != froms)
         return new FromClause(froms);
     return item;
 }
Exemplo n.º 9
0
 public NLinq.Expressions.Expression Visit(ConstantExpression expression)
 {
     if (expression.Value != null && expression.Value.GetType().IsGenericType && (expression.Value.GetType().GetGenericTypeDefinition() == typeof(QueryableUss<>) || expression.Value.GetType().GetGenericTypeDefinition() == typeof(AsyncQueryableUss<>)))
     {
         from = new FromClause(expression.Value.GetType().GetGenericArguments()[0].FullName, this.identifiers["#First"], null);
         return null;
     }
     else
         return new ValueExpression(expression.Value, Utility.Helper.GetTypeCode(expression.Value));
 }
Exemplo n.º 10
0
        public NLinq.Expressions.Expression Visit(ParameterExpression expression)
        {
            if (from == null)
                from = new FromClause(expression.Type.FullName, this.identifiers["#First"], null);

            return identifiers[expression.Name];
        }
Exemplo n.º 11
0
        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;
        }
Exemplo n.º 12
0
 public QueryTranslator(string sourceType)
 {
     identifiers.Add("#First", new NLINQ.Identifier("e0"));
     from = new NLINQ.FromClause(sourceType, identifiers["#First"], null);
     InferredRelationShips = new List<string>();
 }
Exemplo n.º 13
0
 public abstract void Visit(FromClause expression);
Exemplo n.º 14
0
 public override QueryBodyClause Visit(FromClause expression)
 {
     if (expression.Type == target.Type && targetIdentifier == null)
         targetIdentifier = expression.Identifier;
     return base.Visit(expression);
 }
Exemplo n.º 15
0
 public FromClause Update(FromClause original, Identifier identifier, Expression exp, string type)
 {
     if (identifier != original.Identifier || exp != original.Expression || original.Type != type)
         return new FromClause(type, identifier, exp);
     return original;
 }
Exemplo n.º 16
0
 public QueryExpression Update(QueryExpression original, FromClause from, QueryBody body)
 {
     if (from != original.From || body != original.QueryBody)
         return new QueryExpression(from, body);
     return original;
 }