Esempio n. 1
0
        public static WhereClause CreateWhereClause <TObject>(Expression <Func <TObject, bool> > expr) where TObject : DataObject
        {
            var visitor = new LinqVisitor();

            visitor.Visit(expr);
            return(visitor.GeneratedExpression);
        }
Esempio n. 2
0
 public IDbOper <T> Select(Expression <Func <T, object> > sl)
 {
     using (var tp1 = new LinqVisitor())
     {
         var           tp = tp1.VisitNew(sl.Body as NewExpression);
         StringBuilder sb = new StringBuilder();
         foreach (var n in tp)
         {
             sb.Append(Quote(n) + ",");
         }
         if (sb.Length > 0)
         {
             sb.Length--;
         }
         return(Select(sb.ToString()));
     }
 }
Esempio n. 3
0
            protected override Expression VisitBinary(BinaryExpression node)
            {
                switch (node.NodeType)
                {
                case ExpressionType.AndAlso:
                    Visit(node.Left);
                    Visit(node.Right);
                    return(node);

                case ExpressionType.OrElse:
                    var visitor = new LinqVisitor();
                    visitor.Visit(node.Left);
                    visitor.Visit(node.Right);
                    Expressions.Add(visitor.Expressions.First().Or(visitor.Expressions.Last()));
                    return(node);
                }

                string sqlOp;

                if (!ExpressionTypeToSqlOperator.TryGetValue(node.NodeType, out sqlOp))
                {
                    return(node);
                }

                var value = GetValue(node.Right);

                if (value == null && node.NodeType == ExpressionType.Equal)
                {
                    Expressions.Add(DB.Column(GetColumnName(node.Left)).IsNull());
                }
                else if (value == null && node.NodeType == ExpressionType.NotEqual)
                {
                    Expressions.Add(DB.Column(GetColumnName(node.Left)).IsNotNull());
                }
                else
                {
                    Expressions.Add(new FilterExpression(GetColumnName(node.Left), sqlOp, value));
                }

                return(node);
            }
Esempio n. 4
0
        public IDbOper <T> Where(Expression <Func <T, bool> > sl)
        {
            List <object> tp = null; var dc = Fields;

            using (var tp1 = new LinqVisitor())
            {
                tp = tp1.Visit(sl) as List <object>;
                StringBuilder sb = new StringBuilder(); string s = string.Empty;
                var           a = ps.Count;
                for (int i = 0; i < tp.Count; i += 4)
                {
                    s = db.ParStr("Par" + (a + i));// db.ParStr(tp[i].ToString());
                    sb.Append(Quote(tp[i].ToString()) + tp[i + 1].ToString() + s);
                    if (i + 4 < tp.Count)
                    {
                        sb.Append(tp[i + 3]);
                    }
                    ps.Add(db.Cp(s, tp[i + 2]));
                }
                Where(sb.ToString());
            }
            return(this);
        }
Esempio n. 5
0
        public override object Execute(Expression expression)
        {
            //preprocess
            expression = PartialEvaluator.Eval(expression);
            //expression = EnumParameterEvaluator.Eval(expression);
            var linqQuery = LinqVisitor.Eval(expression);

            //get the type constraint
            var types      = _sessionContext.TypeManager.Implementation(typeof(T)).Select(x => $"{x.FullName}, {x.GetTypeInfo().Assembly.GetName().Name}");
            var typesQuery = new QueryObject {
                { "\\$type", new QueryObject {
                      { "$in", types }
                  } }
            };

            var clauses = new List <IDictionary <string, object> >();

            clauses.Add(typesQuery);

            _sessionContext.QueryPart = QueryPart.Where;
            //get all the other where clauses
            foreach (var whereClause in linqQuery.WhereClauses)
            {
                var partial = MongoQueryTransformVisitor.Eval(whereClause, _sessionContext);
                clauses.Add(partial);
            }

            //either take the single where clause or "and" them all together
            IDictionary <string, object> query;

            if (clauses.Count == 1 && !linqQuery.Ordering.Any())
            {
                query = clauses.First();
            }
            else
            {
                query = new QueryObject()
                {
                    { "$and", clauses }
                };
            }

            //sorting / orderby
            _sessionContext.QueryPart = QueryPart.OrderBy;
            var handler = new OrderByHandler(_sessionContext);
            var orders  = new List <IDictionary <string, Order> >();

            foreach (var orderBy in linqQuery.Ordering)
            {
                IDictionary <string, Order> order = new Dictionary <string, Order>();
                var name = handler.GetMemberName((MemberExpression)((LambdaExpression)((UnaryExpression)orderBy.Expression).Operand).Body);
                var or   = orderBy.Direction == OrderByDirection.Asc ? Order.Asc : Order.Desc;
                order.Add(name, or);
                orders.Add(order);

                //add the name to the selector.
                var sortSelector = new QueryObject {
                    { name, new QueryObject {
                          { "$gt", null }
                      } }
                };
                clauses.Add(sortSelector);
            }

            object index = null;

            if (_index != null && _index.Any())
            {
                index = _index.Count() == 1 ? (object)_index.First() : (object)_index;
            }

            var mongoQuery = new MongoQuery()
            {
                Index    = index,
                Selector = query,
                Skip     = linqQuery.Paging.Skip,
                Limit    = linqQuery.Paging.Take,
                Sort     = orders.Count == 0 ? null : orders
            };

            var collection = _session.Query <T>(mongoQuery);

            if (linqQuery.ParentQuery == null)
            {
                return(linqQuery.PostProcess.Execute(collection));
            }

            //we have only run some of the query, this will setup the rest
            linqQuery.ParentQuery.RewriteSource(collection);
            var exp = linqQuery.ParentQuery.Expression;

            //run the expression!
            var result = Expression.Lambda(exp).Compile().DynamicInvoke();

            return(result);
        }