protected override Expression VisitProjection(SqlProjectionExpression projection) { // Visit mapping in reverse order var projector = this.Visit(projection.Projector); var select = (SqlSelectExpression)this.Visit(projection.Select); return(UpdateProjection(projection, select, projector, projection.Aggregator)); }
protected static SqlProjectionExpression UpdateProjection(SqlProjectionExpression projectionExpression, SqlSelectExpression select, Expression projector, LambdaExpression aggregator) { if (select != projectionExpression.Select || projector != projectionExpression.Projector || aggregator != projectionExpression.Aggregator) { return(new SqlProjectionExpression(select, projector, aggregator, projectionExpression.IsElementTableProjection, projectionExpression.DefaultValue)); } return(projectionExpression); }
protected override Expression VisitProjection(SqlProjectionExpression projection) { var saveInProjection = this.inProjection; this.inProjection = true; base.VisitProjection(projection); this.inProjection = saveInProjection; return(projection); }
protected override Expression VisitProjection(SqlProjectionExpression projection) { if (this.hasAggregate) { return(projection); } return(base.VisitProjection(projection)); }
protected override Expression VisitProjection(SqlProjectionExpression projectionExpression) { if (typeof(RelatedDataAccessObjects <>).IsAssignableFromIgnoreGenericParameters(projectionExpression.Type)) { var elementType = projectionExpression.Type.GetGenericArguments()[0]; var originalPlaceholderCount = 0; var currentPlaceholderCount = originalPlaceholderCount; var replacedExpressions = new List <Expression>(); projectionExpression = (SqlProjectionExpression)SqlOuterQueryReferencePlaceholderSubstitutor.Substitute(projectionExpression, ref currentPlaceholderCount, replacedExpressions); var values = replacedExpressions.Select(c => Expression.Convert(this.Visit(c), typeof(object))).ToList(); var where = projectionExpression.Select.Where; var typeDescriptor = this.dataAccessModel.TypeDescriptorProvider.GetTypeDescriptor(elementType); var columns = QueryBinder.GetColumnInfos(this.dataAccessModel.TypeDescriptorProvider, typeDescriptor.PersistedProperties); var columnExpression = (SqlColumnExpression)SqlExpressionFinder.FindFirst(where, c => c.NodeType == (ExpressionType)SqlExpressionType.Column); var match = columns.Single(d => d.ColumnName == columnExpression.Name); var reference = Expression.Call(Expression.Constant(this.dataAccessModel), MethodInfoFastRef.DataAccessModelGetReferenceByValuesMethod.MakeGenericMethod(match.ForeignType.Type), Expression.NewArrayInit(typeof(object), values)); var property = typeDescriptor.GetRelationshipInfos().Single(c => c.ReferencingProperty == match.RootProperty).TargetProperty; return(Expression.Convert(Expression.Property(reference, property), this.dataAccessModel.GetConcreteTypeFromDefinitionType(property.PropertyType))); } else { var currentPlaceholderCount = 0; var replacedExpressions = new List <Expression>(); projectionExpression = (SqlProjectionExpression)SqlOuterQueryReferencePlaceholderSubstitutor.Substitute(projectionExpression, ref currentPlaceholderCount, replacedExpressions); var newColumnIndexes = projectionExpression.Select.Columns.Select((c, i) => new { c.Name, i }).ToDictionary(d => d.Name, d => d.i); var savedScope = this.scope; this.scope = new ProjectionBuilderScope(newColumnIndexes); var projectionProjector = Expression.Lambda(this.Visit(projectionExpression.Projector), objectProjector, dataReader, versionParameter, dynamicParameters); this.scope = savedScope; var values = replacedExpressions.Select(c => (Expression)Expression.Convert(Visit(c), typeof(object))).ToList(); var method = TypeUtils.GetMethod <SqlQueryProvider>(c => c.BuildExecution(default(SqlProjectionExpression), default(LambdaExpression), default(object[]))); MethodInfo evaluate; if (projectionExpression.Type.GetSequenceElementType() == null) { evaluate = MethodInfoFastRef.ExecutionBuildResultEvaluateMethod.MakeGenericMethod(projectionExpression.Type); } else { evaluate = MethodInfoFastRef.ExecutionBuildResultEvaluateMethod.MakeGenericMethod(typeof(IEnumerable <>).MakeGenericType(projectionExpression.Type.GetSequenceElementType())); } return(Expression.Call(Expression.Call(Expression.Property(this.objectProjector, "QueryProvider"), method, Expression.Constant(projectionExpression), projectionProjector, Expression.NewArrayInit(typeof(object), values)), evaluate)); } }
protected override Expression VisitProjection(SqlProjectionExpression projection) { var previousCurrentProjectShouldBeDefaultIfEmpty = this.currentProjectShouldBeDefaultIfEmpty; this.currentProjectShouldBeDefaultIfEmpty = projection.IsDefaultIfEmpty; var retval = this.Visit(projection.Select); this.currentProjectShouldBeDefaultIfEmpty = previousCurrentProjectShouldBeDefaultIfEmpty; return(retval); }
protected override Expression VisitProjection(SqlProjectionExpression projection) { // Visit mapping in reverse order // This collects columns that are used in the projector var projector = Visit(projection.Projector); // Removes columns that aren't used var select = (SqlSelectExpression)Visit(projection.Select); return(UpdateProjection(projection, select, projector, projection.Aggregator)); }
protected override Expression VisitProjection(SqlProjectionExpression projection) { var previousProjection = this.currentProjection; this.currentProjection = projection; var retval = base.VisitProjection(projection); this.currentProjection = previousProjection; return(retval); }
protected override Expression VisitProjection(SqlProjectionExpression projection) { var source = (SqlSelectExpression)Visit(projection.Select); var projector = Visit(projection.Projector); var aggregator = (LambdaExpression)Visit(projection.Aggregator); if (source != projection.Select || projector != projection.Projector || aggregator != projection.Aggregator) { return(new SqlProjectionExpression(source, projector, aggregator, projection.IsElementTableProjection)); } return(projection); }
protected override Expression VisitNew(NewExpression expression) { var projections = new SqlProjectionExpression[expression.Arguments.Count]; for (int i = 0; i < expression.Arguments.Count; i++) { Visit(expression.Arguments[i]); projections[i] = new SqlProjectionExpression((SqlColumnExpression)ast.Pop(), expression.Members[i].Name); } ast.Push(new SqlSelectExpression(projections)); return(expression); }
protected virtual Expression VisitProjection(SqlProjectionExpression projection) { var source = (SqlSelectExpression)this.Visit(projection.Select); var projector = this.Visit(projection.Projector); var defaulValueExpression = this.Visit(projection.DefaultValueExpression); var aggregator = (LambdaExpression)this.Visit(projection.Aggregator); if (source != projection.Select || projector != projection.Projector || defaulValueExpression != projection.DefaultValueExpression || aggregator != projection.Aggregator) { return(new SqlProjectionExpression(source, projector, aggregator, projection.IsElementTableProjection, projection.SelectFirstType, projection.DefaultValueExpression, projection.IsDefaultIfEmpty)); } return(projection); }
protected override Expression VisitProjection(SqlProjectionExpression projection) { var saveProjection = this.currentProjection; try { this.currentProjection = projection; return(base.VisitProjection(projection)); } finally { this.currentProjection = saveProjection; } }
protected override Expression VisitProjection(SqlProjectionExpression projection) { projection = (SqlProjectionExpression)base.VisitProjection(projection); if (projection.Select.From is SqlSelectExpression) { var redundantQueries = SqlRedundantSubqueryFinder.Find(projection.Select); if (redundantQueries != null) { projection = SubqueryRemover.Remove(projection, redundantQueries); } } return(projection); }
protected virtual Expression VisitProjection(SqlProjectionExpression projection) { var source = (SqlSelectExpression)Visit(projection.Select); var projector = Visit(projection.Projector); var defaulValueExpression = Visit(projection.DefaultValue); var aggregator = (LambdaExpression)Visit(projection.Aggregator); var defaultValue = Visit(projection.DefaultValue); if (source != projection.Select || projector != projection.Projector || defaulValueExpression != projection.DefaultValue || aggregator != projection.Aggregator || defaultValue != projection.DefaultValue) { return(new SqlProjectionExpression(projection.Type, source, projector, aggregator, projection.IsElementTableProjection, projection.DefaultValue)); } return(projection); }
protected override Expression VisitFunctionCall(SqlFunctionCallExpression functionCallExpression) { if (functionCallExpression.Function == SqlFunction.In) { if (functionCallExpression.Arguments[0].NodeType == ExpressionType.Constant && functionCallExpression.Arguments[1].NodeType == (ExpressionType)SqlExpressionType.Projection) { var projector = (SqlProjectionExpression)functionCallExpression.Arguments[1]; if (projector.Select.Where == null && projector.Select.Columns.Count == 1) { var newWhere = Expression.Equal(functionCallExpression.Arguments[0], projector.Select.Columns[0].Expression); var newSelect = projector.Select.ChangeWhere(newWhere); var newProjection = new SqlProjectionExpression(newSelect, projector, projector.Aggregator); return(new SqlFunctionCallExpression(functionCallExpression.Type, SqlFunction.Exists, newProjection)); } } } return(base.VisitFunctionCall(functionCallExpression)); }
protected virtual SqlExpression Visit(SqlProjectionExpression expression) { return(new SqlProjectionExpression((SqlColumnExpression)Visit(expression.From), expression.To)); }
public ProjectorExpressionCacheInfo(SqlProjectionExpression projectionExpression, SqlQueryFormatResult formatResult) : this() { this.projectionExpression = projectionExpression; this.formatResult = formatResult; }
string FormatProjection(SqlProjectionExpression expression) { return(string.Format("{0} AS {1}", expression.From.ColumnName, expression.To)); }
private SqlNullComparisonCoalescer(SqlProjectionExpression rootProjection) { this.ignoreExpression = rootProjection?.Projector; }
public static SqlProjectionExpression Remove(SqlProjectionExpression projection, IEnumerable <SqlSelectExpression> selectsToRemove) { return((SqlProjectionExpression) new SqlSubqueryRemover(selectsToRemove).Visit(projection)); }
public static SqlProjectionExpression Remove(SqlProjectionExpression projection, params SqlSelectExpression[] selectsToRemove) { return(Remove(projection, (IEnumerable <SqlSelectExpression>)selectsToRemove)); }