/// <summary> /// Finds the specified expression. /// </summary> /// <param name="expression">The expression.</param> /// <returns></returns> public static MappingEntity Find(Expression expression) { var finder = new EntityFinder(); finder.Visit(expression); return(finder.entity); }
/// <summary> /// Executes the projection. /// </summary> /// <param name="projection">The projection.</param> /// <param name="okayToDefer">if set to <c>true</c> [okay to defer].</param> /// <param name="command">The command.</param> /// <param name="values">The values.</param> /// <returns></returns> private Expression ExecuteProjection(ProjectionExpression projection, bool okayToDefer, QueryCommand command, Expression[] values) { okayToDefer &= (this.receivingMember != null && this.policy.IsDeferLoaded(this.receivingMember)); var saveScope = this.scope; ParameterExpression reader = Expression.Parameter(typeof(FieldReader), "r" + nReaders++); this.scope = new Scope(this.scope, reader, projection.Select.Alias, projection.Select.Columns); LambdaExpression projector = Expression.Lambda(this.Visit(projection.Projector), reader); this.scope = saveScope; var entity = EntityFinder.Find(projection.Projector); string methExecute = okayToDefer ? "ExecuteDeferred" : "Execute"; // call low-level execute directly on supplied DbQueryProvider Expression result = Expression.Call(this.executor, methExecute, new Type[] { projector.Body.Type }, Expression.Constant(command), projector, Expression.Constant(entity, typeof(MappingEntity)), Expression.NewArrayInit(typeof(object), values)); if (projection.Aggregator != null) { // apply aggregator result = DbExpressionReplacer.Replace(projection.Aggregator.Body, projection.Aggregator.Parameters[0], result); } return(result); }
/// <summary> /// Builds the execute batch. /// </summary> /// <param name="batch">The batch.</param> /// <returns></returns> protected virtual Expression BuildExecuteBatch(BatchExpression batch) { // parameterize query Expression operation = this.Parameterize(batch.Operation.Body); string commandText = this.linguist.Format(operation); var namedValues = NamedValueGatherer.Gather(operation); QueryCommand command = new QueryCommand(commandText, namedValues.Select(v => new QueryParameter(v.Name, v.Type))); Expression[] values = namedValues.Select(v => Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray(); Expression paramSets = Expression.Call(typeof(Enumerable), "Select", new Type[] { batch.Operation.Parameters[1].Type, typeof(object[]) }, batch.Input, Expression.Lambda(Expression.NewArrayInit(typeof(object), values), new[] { batch.Operation.Parameters[1] })); Expression plan = null; ProjectionExpression projection = ProjectionFinder.FindProjection(operation); if (projection != null) { var saveScope = this.scope; ParameterExpression reader = Expression.Parameter(typeof(FieldReader), "r" + nReaders++); this.scope = new Scope(this.scope, reader, projection.Select.Alias, projection.Select.Columns); LambdaExpression projector = Expression.Lambda(this.Visit(projection.Projector), reader); this.scope = saveScope; var entity = EntityFinder.Find(projection.Projector); command = new QueryCommand(command.CommandText, command.Parameters); plan = Expression.Call(this.executor, "ExecuteBatch", new Type[] { projector.Body.Type }, Expression.Constant(command), paramSets, projector, Expression.Constant(entity, typeof(MappingEntity)), batch.BatchSize, batch.Stream ); } else { plan = Expression.Call(this.executor, "ExecuteBatch", null, Expression.Constant(command), paramSets, batch.BatchSize, batch.Stream); } return(plan); }