protected virtual Expression BuildExecuteCommand(DbStatementExpression command) { // parameterize query var expression = this.Parameterize(command); string commandText = this.mapping.Language.Format(expression); ReadOnlyCollection <DbNamedValueExpression> namedValues = NamedValueGatherer.Gather(expression); QueryCommand qc = new QueryCommand(commandText, namedValues.Select(v => new QueryParameter(v.Name, v.Type, v.DbType)), null); Expression[] values = namedValues.Select(v => Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray(); DbProjectionExpression projection = ProjectionFinder.FindProjection(expression); if (projection != null) { return(this.ExecuteProjection(projection, false, qc, values)); } Expression plan = Expression.Call(this.provider, "ExecuteCommand", null, Expression.Constant(qc), Expression.NewArrayInit(typeof(object), values) ); return(plan); }
protected virtual Expression BuildExecuteBatch(BatchExpression batch) { // parameterize query Expression operation = this.Parameterize(batch.Operation.Body); string commandText = this.mapping.Language.Format(operation); var namedValues = NamedValueGatherer.Gather(operation); QueryCommand command = new QueryCommand(commandText, namedValues.Select(v => new QueryParameter(v.Name, v.Type, v.DbType)), null); 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; DbProjectionExpression projection = ProjectionFinder.FindProjection(operation); if (projection != null) { var saveScope = this.scope; ParameterExpression reader = Expression.Parameter(typeof(IDataReader), "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 columns = ColumnGatherer.Gather(projection.Projector); command = new QueryCommand(command.CommandText, command.Parameters, columns); plan = Expression.Call(this.provider, "ExecuteBatch", new Type[] { projector.Body.Type }, Expression.Constant(command), paramSets, projector, batch.Size, batch.Stream ); } else { plan = Expression.Call(this.provider, "ExecuteBatch", null, Expression.Constant(command), paramSets, batch.Size, batch.Stream ); } return(plan); }
private Expression ExecuteProjection(DbProjectionExpression projection, bool okayToDefer) { // parameterize query projection = (DbProjectionExpression)this.Parameterize(projection); if (this.scope != null) { // also convert references to outer alias to named values! these become SQL parameters too projection = (DbProjectionExpression)OuterParameterizer.Parameterize(this.scope.Alias, projection); } string commandText = this.mapping.Language.Format(projection.Select); ReadOnlyCollection <DbNamedValueExpression> namedValues = NamedValueGatherer.Gather(projection.Select); QueryCommand command = new QueryCommand(commandText, namedValues.Select(v => new QueryParameter(v.Name, v.Type, v.DbType)), null); Expression[] values = namedValues.Select(v => Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray(); return(this.ExecuteProjection(projection, okayToDefer, command, values)); }