private Implementations?CalculateExpressionImplementations(PropertyRoute route) { if (route.PropertyRouteType != PropertyRouteType.FieldOrProperty) { return(null); } var lambda = ExpressionCleaner.GetFieldExpansion(route.Parent.Type, route.PropertyInfo); if (lambda == null) { return(null); } Expression e = MetadataVisitor.JustVisit(lambda, new MetaExpression(route.Parent.Type, new CleanMeta(route.Parent.TryGetImplementations(), new[] { route.Parent }))); MetaExpression me = e as MetaExpression; if (me == null) { return(null); } return(me.Meta.Implementations); }
public static Expression Clean(Expression expression) { Expression expand = ExpressionCleaner.Clean(expression, MetaEvaluator.PartialEval, false); Expression simplified = OverloadingSimplifier.Simplify(expand); return(simplified); }
protected override Expression VisitMethodCall(MethodCallExpression node) { var obj = base.Visit(node.Object); var args = base.Visit(node.Arguments); LambdaExpression?lambda = ExpressionCleaner.GetFieldExpansion(obj?.Type, node.Method); if (lambda != null) { var replace = ExpressionReplacer.Replace(Expression.Invoke(lambda, obj == null ? args : args.PreAnd(obj))); return(this.Visit(replace)); } if (node.Method.Name == "ToString" && node.Arguments.IsEmpty() && obj is CachedEntityExpression ce) { var table = (Table)ce.Constructor.table; if (table.ToStrColumn == null) { throw new InvalidOperationException("Impossible to get ToStrColumn from " + ce.ToString()); } return(BindMember(ce, (FieldValue)table.ToStrColumn, null)); } return(node.Update(obj, args)); }
protected override Expression VisitConstant(ConstantExpression c) { if (disableQueryFilter) { return(base.VisitConstant(c)); } if (typeof(IQueryable).IsAssignableFrom(c.Type)) { IQueryable query = (IQueryable)c.Value; if (query.IsBase()) { Type queryType = c.Type.GetGenericArguments().SingleEx(); if (filter) { if (typeof(Entity).IsAssignableFrom(queryType)) { LambdaExpression?rawFilter = giFilter.GetInvoker(queryType)(Schema.Current); if (rawFilter != null) { Expression clean = ExpressionCleaner.Clean(rawFilter) !; var cleanFilter = (LambdaExpression)OverloadingSimplifier.Simplify(clean) !; return(Expression.Call(miWhere.MakeGenericMethod(queryType), query.Expression, cleanFilter)); } } else if (queryType.IsInstantiationOf(typeof(MListElement <,>))) { Type entityType = queryType.GetGenericArguments()[0]; LambdaExpression?rawFilter = giFilter.GetInvoker(entityType)(Schema.Current); if (rawFilter != null) { var param = Expression.Parameter(queryType, "mle"); var lambda = Expression.Lambda(Expression.Invoke(rawFilter, Expression.Property(param, "Parent")), param); Expression clean = ExpressionCleaner.Clean(lambda) !; var cleanFilter = (LambdaExpression)OverloadingSimplifier.Simplify(clean) !; return(Expression.Call(miWhere.MakeGenericMethod(queryType), query.Expression, cleanFilter)); } } } return(c); } else { /// <summary> /// Replaces every expression like ConstantExpression{ Type = IQueryable, Value = complexExpr } by complexExpr /// </summary> return(DbQueryProvider.Clean(query.Expression, filter, null) !); } } return(base.VisitConstant(c)); }
private Expression <Func <T, IEnumerable <KVP> > > CombineSelectors <T, M, KVP>(Expression <Func <T, M> > embeddedSelector, Expression <Func <M, IEnumerable <KVP> > > collectionSelector) where T : Entity where M : ModifiableEntity { Expression <Func <T, IEnumerable <KVP> > > result = e => collectionSelector.Evaluate(embeddedSelector.Evaluate(e)); return((Expression <Func <T, IEnumerable <KVP> > >)ExpressionCleaner.Clean(result) !); }
protected override Expression VisitMethodCall(MethodCallExpression node) { if (node.Method.DeclaringType == typeof(string) && node.Method.Name == nameof(string.Format) || node.Method.DeclaringType == typeof(StringExtensions) && node.Method.Name == nameof(StringExtensions.FormatWith)) { var formatStr = Visit(node.Arguments[0]); var remainging = node.Arguments.Skip(1).Select(a => Visit(ToString(a))).ToList(); return(node.Update(null, new Sequence <Expression> { formatStr, remainging })); } var obj = base.Visit(node.Object); var args = base.Visit(node.Arguments); if (node.Method.Name == "ToString" && node.Arguments.IsEmpty() && obj is CachedEntityExpression ce && ce.Type.IsEntity()) { var table = (Table)ce.Constructor.table; if (table.ToStrColumn != null) { return(BindMember(ce, (FieldValue)table.ToStrColumn, null)); } else if (this.root != ce) { var cachedTableType = typeof(CachedTable <>).MakeGenericType(table.Type); ConstantExpression tab = Expression.Constant(ce.Constructor.cachedTable, cachedTableType); var mi = cachedTableType.GetMethod(nameof(CachedTable <Entity> .GetToString)); return(Expression.Call(tab, mi, ce.PrimaryKey.UnNullify())); } } LambdaExpression?lambda = ExpressionCleaner.GetFieldExpansion(obj?.Type, node.Method); if (lambda != null) { var replace = ExpressionReplacer.Replace(Expression.Invoke(lambda, obj == null ? args : args.PreAnd(obj))); return(this.Visit(replace)); } if (node.Method.Name == nameof(Entity.Mixin) && obj is CachedEntityExpression cee) { var mixin = ((Table)cee.Constructor.table).GetField(node.Method); return(GetField(mixin, cee.Constructor, cee.PrimaryKey)); } return(node.Update(obj, args)); }
public static Expression?Clean(Expression?expr, Func <Expression, Expression> partialEval, bool shortCircuit) { ExpressionCleaner ee = new ExpressionCleaner(partialEval, shortCircuit); var result = ee.Visit(expr); if (result == null) { return(null); } return(partialEval(result)); }
public static Expression?Clean(Expression?expression, bool filter, HeavyProfiler.Tracer?log) { Expression?clean = ExpressionCleaner.Clean(expression); log.Switch("OvrLdSmp"); Expression?simplified = OverloadingSimplifier.Simplify(clean); log.Switch("QrFlr"); Expression?filtered = QueryFilterer.Filter(simplified, filter); return(filtered); }
public static Dictionary <string, TypeInfoTS> GetEntities(IEnumerable <Type> allTypes) { var models = (from type in allTypes where typeof(ModelEntity).IsAssignableFrom(type) && !type.IsAbstract select type).ToList(); var dqm = DynamicQueryManager.Current; var settings = Schema.Current.Settings; var result = (from type in TypeLogic.TypeToEntity.Keys.Concat(models) where !type.IsEnumEntity() && !ReflectionServer.ExcludeTypes.Contains(type) let descOptions = LocalizedAssembly.GetDescriptionOptions(type) select KVP.Create(GetTypeName(type), OnAddTypeExtension(new TypeInfoTS { Kind = KindOfType.Entity, FullName = type.FullName, NiceName = descOptions.HasFlag(DescriptionOptions.Description) ? type.NiceName() : null, NicePluralName = descOptions.HasFlag(DescriptionOptions.PluralDescription) ? type.NicePluralName() : null, Gender = descOptions.HasFlag(DescriptionOptions.Gender) ? type.GetGender().ToString() : null, EntityKind = type.IsIEntity() ? EntityKindCache.GetEntityKind(type) : (EntityKind?)null, EntityData = type.IsIEntity() ? EntityKindCache.GetEntityData(type) : (EntityData?)null, IsLowPopulation = type.IsIEntity() ? EntityKindCache.IsLowPopulation(type) : false, ToStringFunction = LambdaToJavascriptConverter.ToJavascript(ExpressionCleaner.GetFieldExpansion(type, miToString)), QueryDefined = dqm.QueryDefined(type), Members = PropertyRoute.GenerateRoutes(type).Where(pr => InTypeScript(pr)) .ToDictionary(p => p.PropertyString(), p => { var mi = new MemberInfoTS { NiceName = p.PropertyInfo?.NiceName(), TypeNiceName = GetTypeNiceName(p.PropertyInfo?.PropertyType), Format = p.PropertyRouteType == PropertyRouteType.FieldOrProperty ? Reflector.FormatString(p) : null, IsReadOnly = !IsId(p) && (p.PropertyInfo?.IsReadOnly() ?? false), Unit = p.PropertyInfo?.GetCustomAttribute <UnitAttribute>()?.UnitName, Type = new TypeReferenceTS(IsId(p) ? PrimaryKey.Type(type).Nullify() : p.PropertyInfo?.PropertyType, p.Type.IsMList() ? p.Add("Item").TryGetImplementations() : p.TryGetImplementations()), IsMultiline = Validator.TryGetPropertyValidator(p)?.Validators.OfType <StringLengthValidatorAttribute>().FirstOrDefault()?.MultiLine ?? false, MaxLength = Validator.TryGetPropertyValidator(p)?.Validators.OfType <StringLengthValidatorAttribute>().FirstOrDefault()?.Max.DefaultToNull(-1), PreserveOrder = settings.FieldAttributes(p)?.OfType <PreserveOrderAttribute>().Any() ?? false, }; return(OnAddPropertyRouteExtension(mi, p)); }), Operations = !type.IsEntity() ? null : OperationLogic.GetAllOperationInfos(type) .ToDictionary(oi => oi.OperationSymbol.Key, oi => OnAddOperationExtension(new OperationInfoTS(oi), oi, type)) }, type))).ToDictionaryEx("entities"); return(result); }
public static string?ToJavascript(LambdaExpression lambdaExpression) { if (lambdaExpression == null) { return(null); } var newLambda = (LambdaExpression)ExpressionCleaner.Clean(lambdaExpression) !; var body = ToJavascript(newLambda.Parameters.Single(), newLambda.Body); if (body == null) { return(null); } return("function(e){ return " + body + "; }"); }
private Expression CallToString(Expression expression) { if (expression.Type == typeof(string)) { return(expression); } if (expression is ConstantExpression c && c.Value != null) { return(Expression.Call(expression, miToString)); } var toStrExp = ExpressionCleaner.Clean(Expression.Call(expression, miToString)); var visitedToStrExp = Visit(toStrExp); return(Expression.Condition( Expression.Equal(expression, Expression.Constant(null, expression.Type.Nullify())), Expression.Constant(null, typeof(string)), Visit(visitedToStrExp))); }
public static Expression GetToString(Table table, ParameterExpression reader, List <IColumn> columns) { LambdaExpression lambda = ExpressionCleaner.GetFieldExpansion(table.Type, CachedTableBase.ToStringMethod) !; if (lambda == null) { columns.Add(table.ToStrColumn !); return(FieldReader.GetExpression(reader, columns.Count - 1, typeof(string))); } ToStringExpressionVisitor toStr = new ToStringExpressionVisitor( lambda.Parameters.SingleEx(), reader, columns, table ); var result = toStr.Visit(lambda.Body); return(result); }
public static bool QueryableProperty(Type type, PropertyInfo pi) { QueryablePropertyAttribute spa = pi.GetCustomAttribute <QueryablePropertyAttribute>(); if (spa != null) { return(spa.AvailableForQueries); } FieldInfo fi = TryFindFieldInfo(type, pi); if (fi != null && !fi.HasAttribute <IgnoreAttribute>() && !pi.HasAttribute <IgnoreAttribute>()) { return(true); } if (ExpressionCleaner.HasExpansions(type, pi)) { return(true); } return(false); }
public static Condition ToCondition(Expression <Func <AutomationElement, bool> > condition) { Expression <Func <AutomationElement, bool> > clean = (Expression <Func <AutomationElement, bool> >)ExpressionCleaner.Clean(condition); ConditionBuilder builder = new ConditionBuilder { parameter = clean.Parameters.Single() }; Expression expression = builder.Visit(clean.Body); return(AsCondition(expression)); }
public static Dictionary <string, TypeInfoTS> GetEntities(IEnumerable <Type> allTypes) { var models = (from type in allTypes where typeof(ModelEntity).IsAssignableFrom(type) && !type.IsAbstract select type).ToList(); var queries = QueryLogic.Queries; var schema = Schema.Current; var settings = Schema.Current.Settings; var result = (from type in TypeLogic.TypeToEntity.Keys.Concat(models) where !type.IsEnumEntity() && !ReflectionServer.ExcludeTypes.Contains(type) let descOptions = LocalizedAssembly.GetDescriptionOptions(type) let allOperations = !type.IsEntity() ? null : OperationLogic.GetAllOperationInfos(type) select KeyValuePair.Create(GetTypeName(type), OnAddTypeExtension(new TypeInfoTS { Kind = KindOfType.Entity, FullName = type.FullName !, NiceName = descOptions.HasFlag(DescriptionOptions.Description) ? type.NiceName() : null, NicePluralName = descOptions.HasFlag(DescriptionOptions.PluralDescription) ? type.NicePluralName() : null, Gender = descOptions.HasFlag(DescriptionOptions.Gender) ? type.GetGender().ToString() : null, EntityKind = type.IsIEntity() ? EntityKindCache.GetEntityKind(type) : (EntityKind?)null, EntityData = type.IsIEntity() ? EntityKindCache.GetEntityData(type) : (EntityData?)null, IsLowPopulation = type.IsIEntity() ? EntityKindCache.IsLowPopulation(type) : false, IsSystemVersioned = type.IsIEntity() ? schema.Table(type).SystemVersioned != null : false, ToStringFunction = typeof(Symbol).IsAssignableFrom(type) ? null : LambdaToJavascriptConverter.ToJavascript(ExpressionCleaner.GetFieldExpansion(type, miToString) !), QueryDefined = queries.QueryDefined(type), Members = PropertyRoute.GenerateRoutes(type) .Where(pr => InTypeScript(pr)) .ToDictionary(p => p.PropertyString(), p => { var validators = Validator.TryGetPropertyValidator(p)?.Validators; var mi = new MemberInfoTS { NiceName = p.PropertyInfo !.NiceName(), TypeNiceName = GetTypeNiceName(p.PropertyInfo !.PropertyType), Format = p.PropertyRouteType == PropertyRouteType.FieldOrProperty ? Reflector.FormatString(p) : null, IsReadOnly = !IsId(p) && (p.PropertyInfo?.IsReadOnly() ?? false), Required = !IsId(p) && ((p.Type.IsValueType && !p.Type.IsNullable()) || (validators?.Any(v => !v.DisabledInModelBinder && (!p.Type.IsMList() ? (v is NotNullValidatorAttribute) : (v is CountIsValidatorAttribute c && c.IsGreaterThanZero))) ?? false)), Unit = UnitAttribute.GetTranslation(p.PropertyInfo?.GetCustomAttribute <UnitAttribute>()?.UnitName), Type = new TypeReferenceTS(IsId(p) ? PrimaryKey.Type(type).Nullify() : p.PropertyInfo !.PropertyType, p.Type.IsMList() ? p.Add("Item").TryGetImplementations() : p.TryGetImplementations()), IsMultiline = validators?.OfType <StringLengthValidatorAttribute>().FirstOrDefault()?.MultiLine ?? false, IsVirtualMList = p.IsVirtualMList(), MaxLength = validators?.OfType <StringLengthValidatorAttribute>().FirstOrDefault()?.Max.DefaultToNull(-1), PreserveOrder = settings.FieldAttributes(p)?.OfType <PreserveOrderAttribute>().Any() ?? false, }; return(OnAddPropertyRouteExtension(mi, p)); }),
public IQueryable <S> CreateQuery <S>(Expression expression) { Expression res = ExpressionCleaner.Clean(expression) !; return(new ExpandableQueryProvider <S>(_item.Provider.CreateQuery <S>(res))); }
public object?Execute(Expression expression) { return(_item.Provider.Execute(ExpressionCleaner.Clean(expression) !)); }
protected Dictionary <string, EntityField> GenerateFields(PropertyRoute root, ITable table, NameSequence preName, bool forceNull, bool inMList) { Dictionary <string, EntityField> result = new Dictionary <string, EntityField>(); var type = root.Type; if (type.IsEntity()) { { PropertyRoute route = root.Add(fiId); Field field = GenerateField(table, route, preName, forceNull, inMList); result.Add(fiId.Name, new EntityField(type, fiId) { Field = field }); } TicksColumnAttribute t = type.GetCustomAttribute <TicksColumnAttribute>(); if (t == null || t.HasTicks) { PropertyRoute route = root.Add(fiTicks); Field field = GenerateField(table, route, preName, forceNull, inMList); result.Add(fiTicks.Name, new EntityField(type, fiTicks) { Field = field }); } Expression exp = ExpressionCleaner.GetFieldExpansion(type, EntityExpression.ToStringMethod); if (exp == null) { PropertyRoute route = root.Add(fiToStr); Field field = GenerateField(table, route, preName, forceNull, inMList); if (result.ContainsKey(fiToStr.Name)) { throw new InvalidOperationException("Duplicated field with name {0} on {1}, shadowing not supported".FormatWith(fiToStr.Name, type.TypeName())); } result.Add(fiToStr.Name, new EntityField(type, fiToStr) { Field = field }); } } foreach (FieldInfo fi in Reflector.InstanceFieldsInOrder(type)) { PropertyRoute route = root.Add(fi); if (Settings.FieldAttribute <IgnoreAttribute>(route) == null) { if (Reflector.TryFindPropertyInfo(fi) == null && !fi.IsPublic && !fi.HasAttribute <FieldWithoutPropertyAttribute>()) { throw new InvalidOperationException("Field '{0}' of type '{1}' has no property".FormatWith(fi.Name, type.Name)); } Field field = GenerateField(table, route, preName, forceNull, inMList); if (result.ContainsKey(fi.Name)) { throw new InvalidOperationException("Duplicated field with name '{0}' on '{1}', shadowing not supported".FormatWith(fi.Name, type.TypeName())); } result.Add(fi.Name, new EntityField(type, fi) { Field = field }); } } return(result); }
private static bool IsExpression(Type type, PropertyInfo propertyInfo) { return(propertyInfo.SetMethod == null && ExpressionCleaner.HasExpansions(type, propertyInfo)); }