Beispiel #1
0
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {   
            var result = Parent.BuildExpression(context);

            var prop = Expression.Property(result.UnNullify(), PropertyInfo);

            return Expression.Call(miInSql.MakeGenericMethod(prop.Type), prop).Nullify();
        }
Beispiel #2
0
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var parentResult = Parent.BuildExpression(context);

            var result = Expression.Call(miCount.MakeGenericMethod(parentResult.Type.ElementType()), parentResult);

            return result.Nullify().BuildLite();
        }
Beispiel #3
0
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var baseExpression = Parent.BuildExpression(context);

            Expression result = Expression.Property(baseExpression, PropertyInfo);

            return result.BuildLite().Nullify();
        }
Beispiel #4
0
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            Expression baseExpression = Parent.BuildExpression(context);

            Expression result = Expression.TypeAs(baseExpression.ExtractEntity(false), entityType);

            return result.BuildLite();
        }
Beispiel #5
0
        private Expression GetConditionBasic(BuildExpressionContext context)
        {
            Expression left = Token.BuildExpression(context);

            if (Operation == FilterOperation.IsIn)
            {
                if (Value == null)
                    return Expression.Constant(false);

                IList clone = (IList)Activator.CreateInstance(Value.GetType(), Value);

                bool hasNull = false;
                while (clone.Contains(null))
                {
                    clone.Remove(null);
                    hasNull = true;
                }

                if (token.Type == typeof(string))
                {
                    while (clone.Contains(""))
                    {
                        clone.Remove("");
                        hasNull = true;
                    }

                    if (hasNull)
                    {
                        clone.Add("");
                        left = Expression.Coalesce(left, Expression.Constant(""));
                    }
                }


                Expression right = Expression.Constant(clone, typeof(IEnumerable<>).MakeGenericType(Token.Type.Nullify()));
                var contains =  Expression.Call(miContainsEnumerable.MakeGenericMethod(Token.Type.Nullify()), right, left.Nullify());

                if (!hasNull || token.Type == typeof(string))
                    return contains;

                return Expression.Or(Expression.Equal(left, Expression.Constant(null, Token.Type.Nullify())), contains);
            }
            else
            {
                var val = Value;
                if (token.Type == typeof(string) && (val == null || val is string && string.IsNullOrEmpty((string)val)))
                {
                    val = val ?? "";
                    left = Expression.Coalesce(left, Expression.Constant(""));
                }

                Expression right = Expression.Constant(val, Token.Type);

                return QueryUtils.GetCompareExpression(Operation, left, right);
            }
        }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var exp = Parent.BuildExpression(context);

            var call = exp.Type.UnNullify() == typeof(decimal) ?
                Expression.Call(miFloorDecimal, exp.UnNullify()) :
                Expression.Call(miFloorDouble, Expression.Convert(exp, typeof(double)));

            return Expression.Convert(call.Nullify(), typeof(int?));
        }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            Expression baseExpression = Parent.BuildExpression(context);
            
            var result = Expression.NotEqual(baseExpression, Expression.Constant(null, baseExpression.Type.Nullify()));

            if (baseExpression.Type == typeof(string))
                result = Expression.And(result, Expression.NotEqual(baseExpression, Expression.Constant("")));

            return result;
        }
Beispiel #8
0
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            if (BuildExtension == null)
                throw new InvalidOperationException("ExtensionToken.BuildExtension not set");

            var parentExpression = Parent.BuildExpression(context).ExtractEntity(false).UnNullify();

            var result = BuildExtension(Parent.Type.CleanType().UnNullify(), Key, parentExpression);

            return result.BuildLiteNulifyUnwrapPrimaryKey(new[] { this.propertyRoute });
        }
Beispiel #9
0
        static Expression <Func <object, object> > TupleConstructor(BuildExpressionContext context, HashSet <QueryToken> tokens, out BuildExpressionContext newContext)
        {
            string str = tokens.Select(t => QueryUtils.CanColumn(t)).NotNull().ToString("\r\n");

            if (str == null)
            {
                throw new ApplicationException(str);
            }

            List <Expression> expressions = tokens.Select(t => t.BuildExpression(context)).ToList();
            Expression        ctor        = TupleReflection.TupleChainConstructor(expressions);

            var pe = Expression.Parameter(typeof(object));

            newContext = new BuildExpressionContext(
                ctor.Type, pe,
                tokens.Select((t, i) => new { Token = t, Expr = TupleReflection.TupleChainProperty(Expression.Convert(pe, ctor.Type), i) }).ToDictionary(t => t.Token, t => t.Expr));

            return(Expression.Lambda <Func <object, object> >(
                       (Expression)Expression.Convert(ctor, typeof(object)), context.Parameter));
        }
Beispiel #10
0
        static LambdaExpression ResultSelectSelectorAndContext(BuildExpressionContext context, HashSet <QueryToken> keyTokens, HashSet <AggregateToken> aggregateTokens, Type keyTupleType, out BuildExpressionContext newContext)
        {
            Dictionary <QueryToken, Expression> resultExpressions = new Dictionary <QueryToken, Expression>();
            ParameterExpression pk = Expression.Parameter(keyTupleType, "key");

            resultExpressions.AddRange(keyTokens.Select((kt, i) => KVP.Create(kt,
                                                                              TupleReflection.TupleChainProperty(pk, i))));

            ParameterExpression pe = Expression.Parameter(typeof(IEnumerable <object>), "e");

            resultExpressions.AddRange(aggregateTokens.Select(at => KVP.Create((QueryToken)at,
                                                                               BuildAggregateExpression(pe, at, context))));

            var resultConstructor = TupleReflection.TupleChainConstructor(resultExpressions.Values);

            ParameterExpression pg = Expression.Parameter(typeof(object), "gr");

            newContext = new BuildExpressionContext(resultConstructor.Type, pg,
                                                    resultExpressions.Keys.Select((t, i) => KVP.Create(t, TupleReflection.TupleChainProperty(Expression.Convert(pg, resultConstructor.Type), i))).ToDictionary());

            return(Expression.Lambda(Expression.Convert(resultConstructor, typeof(object)), pk, pe));
        }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var baseExpression = Parent.BuildExpression(context);

            if (PropertyInfo.Is((IdentifiableEntity ident) => ident.Id) ||
                PropertyInfo.Is((IdentifiableEntity ident) => ident.ToStringProperty))
            {
                var entityExpression = baseExpression.ExtractEntity(true);

                return Expression.Property(entityExpression, PropertyInfo.Name).Nullify(); // Late binding over Lite or Identifiable
            }
            else
            {
                var entityExpression = baseExpression.ExtractEntity(false);

                if (PropertyRoute != null && PropertyRoute.Parent != null && PropertyRoute.Parent.PropertyRouteType == PropertyRouteType.Mixin)
                    entityExpression = Expression.Call(entityExpression, MixinDeclarations.miMixin.MakeGenericMethod(PropertyRoute.Parent.Type));

                Expression result = Expression.Property(entityExpression, PropertyInfo);

                return result.BuildLite().Nullify();
            }
        }
Beispiel #12
0
        public override Expression GetExpression(BuildExpressionContext ctx)
        {
            CollectionAnyAllToken anyAll = Token.Follow(a => a.Parent)
                                           .OfType <CollectionAnyAllToken>()
                                           .TakeWhile(c => !ctx.Replacemens.ContainsKey(c))
                                           .LastOrDefault();

            if (anyAll == null)
            {
                return(GetConditionExpressionBasic(ctx));
            }

            Expression collection  = anyAll.Parent.BuildExpression(ctx);
            Type       elementType = collection.Type.ElementType();

            var p = Expression.Parameter(elementType, elementType.Name.Substring(0, 1).ToLower());

            ctx.Replacemens.Add(anyAll, p.BuildLiteNulifyUnwrapPrimaryKey(new[] { anyAll.GetPropertyRoute() }));
            var body = GetExpression(ctx);

            ctx.Replacemens.Remove(anyAll);

            return(anyAll.BuildAnyAll(collection, p, body));
        }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var exp = Parent.BuildExpression(context);

            return(Expression.Modulo(Expression.Convert(exp, typeof(int)), Expression.Constant(Divisor)).Nullify());
        }
Beispiel #14
0
        private Expression GetConditionBasic(BuildExpressionContext context)
        {
            Expression left = Token.BuildExpression(context);

            if (Operation.IsList())
            {
                if (Value == null)
                {
                    return(Expression.Constant(false));
                }

                IList clone = (IList)Activator.CreateInstance(Value.GetType(), Value);

                bool hasNull = false;
                while (clone.Contains(null))
                {
                    clone.Remove(null);
                    hasNull = true;
                }

                if (token.Type == typeof(string))
                {
                    while (clone.Contains(""))
                    {
                        clone.Remove("");
                        hasNull = true;
                    }

                    if (hasNull)
                    {
                        clone.Add("");
                        left = Expression.Coalesce(left, Expression.Constant(""));
                    }
                }


                Expression right    = Expression.Constant(clone, typeof(IEnumerable <>).MakeGenericType(Token.Type.Nullify()));
                var        contains = Expression.Call(miContainsEnumerable.MakeGenericMethod(Token.Type.Nullify()), right, left.Nullify());


                var result = !hasNull || token.Type == typeof(string) ? (Expression)contains :
                             Expression.Or(Expression.Equal(left, Expression.Constant(null, Token.Type.Nullify())), contains);


                if (Operation == FilterOperation.IsIn)
                {
                    return(result);
                }

                if (Operation == FilterOperation.IsNotIn)
                {
                    return(Expression.Not(result));
                }

                throw new InvalidOperationException("Unexpected operation");
            }
            else
            {
                var val = Value;
                if (token.Type == typeof(string) && (val == null || val is string && string.IsNullOrEmpty((string)val)))
                {
                    val  = val ?? "";
                    left = Expression.Coalesce(left, Expression.Constant(""));
                }

                Expression right = Expression.Constant(val, Token.Type);

                return(QueryUtils.GetCompareExpression(Operation, left, right));
            }
        }
Beispiel #15
0
 protected override Expression BuildExpressionInternal(BuildExpressionContext context)
 {
     throw new InvalidOperationException("AggregateToken should have a replacement at this stage");
 }
Beispiel #16
0
 public Func <object, T> GetAccessor <T>(BuildExpressionContext context)
 {
     return(Expression.Lambda <Func <object, T> >(this.BuildExpression(context), context.Parameter).Compile());
 }
Beispiel #17
0
 public abstract Expression GetExpression(BuildExpressionContext ctx);
Beispiel #18
0
 protected override Expression BuildExpressionInternal(BuildExpressionContext context)
 {
     throw new InvalidOperationException("AggregateToken should have a replacement at this stage");
 }
Beispiel #19
0
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {   
            var result = Parent.BuildExpression(context);

            return Expression.Property(result.UnNullify(), PropertyInfo).Nullify();
        }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var exp = Parent.Parent.Parent.BuildExpression(context);

            return(RoundingExpressionGenerator.RoundExpression(exp, ((StepMultiplierToken)this.Parent).StepSize(), this.Rounding));
        }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var exp = Parent.BuildExpression(context);

            return Expression.Property(exp.UnNullify(), piDayOfWeek).Nullify();
        }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var exp = Parent.BuildExpression(context);

            return RoundingExpressionGenerator.RoundExpression(exp, this.StepSize, RoundingType.Ceil);
        }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var exp = Parent.Parent.Parent.BuildExpression(context);

            return RoundingExpressionGenerator.RoundExpression(exp, ((StepMultiplierToken)this.Parent).StepSize(), this.Rounding);
        }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var baseExpression = Parent.BuildExpression(context);

            return Expression.Call(baseExpression, miToString); 
        }
 protected override Expression BuildExpressionInternal(BuildExpressionContext context)
 {
     var exp = Parent.BuildExpression(context);
     
     return Expression.Call(miMonthStart, exp.UnNullify()).Nullify();
 }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var baseExpression = Parent.BuildExpression(context);

            return(Expression.Call(baseExpression, miToString));
        }
Beispiel #27
0
 public DQueryable(IQueryable <object> query, BuildExpressionContext context)
 {
     this.Query   = query;
     this.Context = context;
 }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var exp = Parent.BuildExpression(context);

            return(RoundingExpressionGenerator.RoundExpression(exp, this.StepSize, RoundingType.Ceil));
        }
Beispiel #29
0
 public DEnumerable(IEnumerable <object> collection, BuildExpressionContext context)
 {
     this.Collection = collection;
     this.Context    = context;
 }
Beispiel #30
0
 protected override Expression BuildExpressionInternal(BuildExpressionContext context)
 {
     throw new InvalidOperationException("ColumnToken {0} not found on replacements".FormatWith(this));
 }
 public FilterBuildExpressionContext(BuildExpressionContext context)
     : base(context.TupleType, context.Parameter, context.Replacemens.ToDictionary())
 {
 }
Beispiel #32
0
 protected abstract Expression BuildExpressionInternal(BuildExpressionContext context);
Beispiel #33
0
 protected override Expression BuildExpressionInternal(BuildExpressionContext context)
 {
     throw new InvalidOperationException("ColumnToken {0} not found on replacements".FormatWith(this));
 }
Beispiel #34
0
        static Expression BuildAggregateExpression(Expression collection, AggregateToken at, BuildExpressionContext context)
        {
            Type groupType = collection.Type.GetGenericInterfaces(typeof(IEnumerable <>)).SingleEx(() => "IEnumerable<T> implementations on {0}".FormatWith(collection.Type)).GetGenericArguments()[0];

            if (at.AggregateFunction == Signum.Entities.DynamicQuery.AggregateFunction.Count)
            {
                return(Expression.Call(typeof(Enumerable), "Count", new[] { groupType }, new[] { collection }));
            }

            var body = at.Parent.BuildExpression(context);

            var type = at.Type;

            if (body.Type != type)
            {
                body = body.TryConvert(type);
            }

            var lambda = Expression.Lambda(body, context.Parameter);

            if (at.AggregateFunction == Signum.Entities.DynamicQuery.AggregateFunction.Min || at.AggregateFunction == Signum.Entities.DynamicQuery.AggregateFunction.Max)
            {
                return(Expression.Call(typeof(Enumerable), at.AggregateFunction.ToString(), new[] { groupType, lambda.Body.Type }, new[] { collection, lambda }));
            }

            return(Expression.Call(typeof(Enumerable), at.AggregateFunction.ToString(), new[] { groupType }, new[] { collection, lambda }));
        }
Beispiel #35
0
 public FilterBuildExpressionContext(BuildExpressionContext context)
     : base(context.TupleType, context.Parameter, context.Replacemens.ToDictionary())
 {
 }
Beispiel #36
0
 public DQueryableCount(IQueryable <object> query, BuildExpressionContext context, int totalElements) :
     base(query, context)
 {
     this.TotalElements = totalElements;
 }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var exp = Parent.BuildExpression(context);

            return(Expression.Property(exp.UnNullify(), piDayOfWeek).Nullify());
        }
Beispiel #38
0
 public DEnumerableCount(IEnumerable <object> collection, BuildExpressionContext context, int?totalElements) :
     base(collection, context)
 {
     this.TotalElements = totalElements;
 }
        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            var exp = Parent.BuildExpression(context);

            return(Expression.Call(miMonthStart, exp.UnNullify()).Nullify());
        }