Ejemplo n.º 1
0
        protected virtual Expression BuildExecuteCommand(CommandExpression command)
        {
            // parameterize query
            var expression = this.Parameterize(command);

            string commandText = this.linguist.Format(expression);
            ReadOnlyCollection <NamedValueExpression> namedValues = NamedValueGatherer.Gather(expression);
            QueryCommand qc = new QueryCommand(commandText, namedValues.Select(v => new QueryParameter(v.Name, v.Type, v.QueryType)));

            Expression[] values = namedValues.Select(v => Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray();

            ProjectionExpression projection = ProjectionFinder.FindProjection(expression);

            if (projection != null)
            {
                return(this.ExecuteProjection(projection, false, qc, values, isTopLevel: true));
            }

            var plan = Expression.Call(this.executor, nameof(QueryExecutor.ExecuteCommand), null,
                                       Expression.Constant(qc),
                                       Expression.NewArrayInit(typeof(object), values)
                                       );

            return(plan);
        }
Ejemplo n.º 2
0
        private Expression ExecuteProjection(ProjectionExpression projection, bool okayToDefer, bool isTopLevel)
        {
            // parameterize query
            projection = (ProjectionExpression)this.Parameterize(projection);

            if (this.scope != null)
            {
                // also convert references to outer alias to named values!  these become SQL parameters too
                projection = (ProjectionExpression)OuterParameterizer.Parameterize(this.scope.Alias, projection);
            }

            string commandText = this.linguist.Format(projection.Select);
            ReadOnlyCollection <NamedValueExpression> namedValues = NamedValueGatherer.Gather(projection.Select);
            QueryCommand command = new QueryCommand(commandText, namedValues.Select(v => new QueryParameter(v.Name, v.Type, v.QueryType)));

            Expression[] values = namedValues.Select(v => Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray();

            string methExecute = okayToDefer
                ? nameof(QueryExecutor.ExecuteDeferred)
                : nameof(QueryExecutor.Execute);

            var plan = Expression.Call(this.executor, methExecute, new Type[] { projection.Projector.Type },
                                       Expression.Constant(command),
                                       Expression.NewArrayInit(typeof(object), values)
                                       );

            return(plan);
        }
Ejemplo n.º 3
0
        public static ReadOnlyCollection <NamedValueExpression> Gather(Expression expr)
        {
            NamedValueGatherer gatherer = new NamedValueGatherer();

            gatherer.Visit(expr);
            return(gatherer.namedValues.ToList().AsReadOnly());
        }
Ejemplo n.º 4
0
        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, v.QueryType)));

            Expression[] values = namedValues.Select(v => Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray();

            Expression paramSets = Expression.Call(typeof(Enumerable), nameof(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;
                this.scope = new Scope(this.scope, projection.Select.Alias, projection.Select.Columns);
                this.scope = saveScope;

                var entity = EntityFinder.Find(projection.Projector);
                command = new QueryCommand(command.CommandText, command.Parameters);

                plan = Expression.Call(this.executor, nameof(QueryExecutor.ExecuteBatch), new Type[] { projection.Projector.Type },
                                       Expression.Constant(command),
                                       paramSets,
                                       batch.BatchSize,
                                       batch.Stream
                                       );
            }
            else
            {
                plan = Expression.Call(this.executor, nameof(QueryExecutor.ExecuteBatch), null,
                                       Expression.Constant(command),
                                       paramSets,
                                       batch.BatchSize,
                                       batch.Stream
                                       );
            }

            return(plan);
        }