コード例 #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));
            }

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

            return(plan);
        }
コード例 #2
0
            internal static ProjectionExpression FindProjection(Expression expression)
            {
                var finder = new ProjectionFinder();

                finder.Visit(expression);
                return(finder.found);
            }
コード例 #3
0
        protected virtual Expression BuildExecuteBatch(DbBatchExpression batch)
        {
            var operation   = this.Parameterize(batch.Operation.Body);
            var commandText = this.linguist.Format(operation);
            var namedValues = DbNamedValueGatherer.Gather(operation);
            var command     = new QueryCommand(commandText, namedValues.Select(v => new QueryParameter(v.Name, v.Type, v.QueryType)));
            var values      = namedValues.Select(v => Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray() as Expression[];

            var 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] })
                            );

            var plan       = null as Expression;
            var projection = ProjectionFinder.FindProjection(operation);

            if (projection != null)
            {
                var saveScope = this.scope;
                var reader    = Expression.Parameter(typeof(FieldReader), "r" + nReaders++);

                this.scope = new Scope(this.scope, reader, projection.Select.Alias, projection.Select.Columns);

                var 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, nameof(QueryExecutor.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, nameof(QueryExecutor.ExecuteBatch), null,
                    Expression.Constant(command),
                    paramSets,
                    batch.BatchSize,
                    batch.Stream
                       );
            }

            return(plan);
        }
コード例 #4
0
        protected virtual Expression BuildExecuteCommand(CommandExpression command)
        {
            // parameterize query
            var expression = this.Parameterize(command);

            var cdu = (command as CDUCommandExpression);

            string commandText = this.dbContext.BuildSql(expression);
            ReadOnlyCollection <NamedValueExpression> namedValues = NamedValueGatherer.Gather(expression);

            Expression[] values     = namedValues.Select(v => Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray();
            var          parameters = namedValues.Select(v => new NamedParameter(v.Name, v.Type, v.SqlType)).ToArray();

            ProjectionExpression projection = ProjectionFinder.FindProjection(expression);

            if (projection != null)
            {
                return(this.ExecuteProjection(projection, false, commandText, parameters, values));
            }

            bool supportVersionCheck = false;
            var  delete = cdu as DeleteCommand;

            if (delete != null)
            {
                supportVersionCheck = delete.SupportsVersionCheck;
            }
            var update = cdu as UpdateCommand;

            if (update != null)
            {
                supportVersionCheck = update.SupportsVersionCheck;
            }

            var commandContext = Expression.New(
                MethodRepository.CommandContext.New,
                Expression.Constant(commandText),
                Expression.Constant(parameters),
                Expression.NewArrayInit(typeof(object), values),
                Expression.Constant(cdu.Table.Mapping.EntityType, Types.Type),
                Expression.Constant((OperationType)cdu.DbNodeType),
                Expression.Constant(supportVersionCheck),
                Expression.Constant(cdu.Instance))
            ;

            //Expression plan = Expression.Call(this.executor, "ExecuteNonQuery", new Type[] { (command as CDUCommandExpression).Table.Entity.EntityType },
            //    Expression.Constant(commandText),
            //     Expression.Constant(parameters),
            //    Expression.NewArrayInit(typeof(object), values)
            //    );

            Expression plan = Expression.Call(this.executor, "ExecuteNonQuery", null, commandContext);

            return(plan);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        protected virtual Expression BuildExecuteCommand(CommandExpression command)
        {
            Expression expression  = this.Parameterize(command);
            string     commandText = this.linguist.Format(expression);
            ReadOnlyCollection <NamedValueExpression> onlys = NamedValueGatherer.Gather(expression);
            QueryCommand command2 = new QueryCommand(commandText, from v in onlys select new QueryParameter(v.Name, v.Type, v.QueryType));

            Expression[]         values     = (from v in onlys select Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray <UnaryExpression>();
            ProjectionExpression projection = ProjectionFinder.FindProjection(expression);

            if (projection != null)
            {
                return(this.ExecuteProjection(projection, false, command2, values));
            }
            return(Expression.Call(this.executor, "ExecuteCommand", (Type[])null, new Expression[] { Expression.Constant(command2), Expression.NewArrayInit(typeof(object), values) }));
        }
コード例 #7
0
ファイル: ExecutionBuilder.cs プロジェクト: 842549829/Pool
        protected virtual Expression BuildExecuteBatch(BatchExpression batch)
        {
            // parameterize query
            Expression operation = Parameterize(batch.Operation.Body);

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

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

            Expression paramSets = Expression.Call(typeof(Enumerable), "Select", new[] { 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)
            {
                Scope saveScope            = scope;
                ParameterExpression reader = Expression.Parameter(typeof(FieldReader), "r" + nReaders++);
                scope = new Scope(scope, reader, projection.Select.Alias, projection.Select.Columns);
                LambdaExpression projector = Expression.Lambda(Visit(projection.Projector), reader);
                scope = saveScope;

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

                plan = Expression.Call(executor, "ExecuteBatch", new[] { projector.Body.Type }, Expression.Constant(command), paramSets, projector, Expression.Constant(entity, typeof(EntryMapping)),
                                       batch.BatchSize, batch.Stream);
            }
            else
            {
                plan = Expression.Call(executor, "ExecuteBatch", null, Expression.Constant(command), paramSets, batch.BatchSize, batch.Stream);
            }

            return(plan);
        }
コード例 #8
0
        protected virtual Expression BuildExecuteCommand(DbCommandExpression command)
        {
            var expression  = this.Parameterize(command);
            var commandText = this.linguist.Format(expression);
            var namedValues = DbNamedValueGatherer.Gather(expression);
            var qc          = new QueryCommand(commandText, namedValues.Select(v => new QueryParameter(v.Name, v.Type, v.QueryType)));
            var values      = namedValues.Select(v => Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray() as Expression[];

            var 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);
        }
コード例 #9
0
        protected virtual Expression BuildExecuteBatch(BatchExpression batch)
        {
            Expression expression  = this.Parameterize(batch.Operation.Body);
            string     commandText = this.linguist.Format(expression);
            ReadOnlyCollection <NamedValueExpression> onlys = NamedValueGatherer.Gather(expression);
            QueryCommand command = new QueryCommand(commandText, from v in onlys select new QueryParameter(v.Name, v.Type, v.QueryType));

            Expression[]         initializers = (from v in onlys select Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray <UnaryExpression>();
            Expression           expression2  = Expression.Call(typeof(Enumerable), "Select", new Type[] { batch.Operation.Parameters[1].Type, typeof(object[]) }, new Expression[] { batch.Input, Expression.Lambda(Expression.NewArrayInit(typeof(object), initializers), new ParameterExpression[] { batch.Operation.Parameters[1] }) });
            ProjectionExpression expression4  = ProjectionFinder.FindProjection(expression);

            if (expression4 != null)
            {
                Scope scope = this.scope;
                ParameterExpression fieldReader = Expression.Parameter(typeof(FieldReader), "r" + this.nReaders++);
                this.scope = new Scope(this.scope, fieldReader, expression4.Select.Alias, expression4.Select.Columns);
                LambdaExpression expression6 = Expression.Lambda(this.Visit(expression4.Projector), new ParameterExpression[] { fieldReader });
                this.scope = scope;
                MappingEntity entity = EntityFinder.Find(expression4.Projector);
                command = new QueryCommand(command.CommandText, command.Parameters);
                return(Expression.Call(this.executor, "ExecuteBatch", new Type[] { expression6.Body.Type }, new Expression[] { Expression.Constant(command), expression2, expression6, Expression.Constant(entity, typeof(MappingEntity)), batch.BatchSize, batch.Stream }));
            }
            return(Expression.Call(this.executor, "ExecuteBatch", (Type[])null, new Expression[] { Expression.Constant(command), expression2, batch.BatchSize, batch.Stream }));
        }
コード例 #10
0
        protected virtual Expression BuildExecuteBatch(BatchExpression batch)
        {
            Expression operation = this.Parameterize(batch.Operation.Body);

            var  cdu        = batch.Operation.Body as CDUCommandExpression;
            Type entityType = null;

            if (cdu != null)
            {
                entityType = cdu.Table.Mapping.EntityType;
            }

            string commandText = this.dbContext.BuildSql(operation);
            var    namedValues = NamedValueGatherer.Gather(operation);
            var    parameters  = namedValues.Select(v => new NamedParameter(v.Name, v.Type, v.SqlType)).ToArray();

            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);

                var batchContext = Expression.New(typeof(BatchContext <>).MakeGenericType(projector.Body.Type).GetConstructors().FirstOrDefault(),
                                                  Expression.Constant(commandText),
                                                  Expression.Constant(parameters),
                                                  paramSets,
                                                  projector);
                plan = Expression.Call(this.executor, "Batch", new Type[] { projector.Body.Type }, batchContext);
            }
            else
            {
                var batchContext = Expression.New(typeof(BatchContext).GetConstructors().FirstOrDefault(),
                                                  Expression.Constant(commandText),
                                                  Expression.Constant(parameters),
                                                  paramSets,
                                                  Expression.Constant(entityType));

                //plan = Expression.Call(this.executor, "Batch", null,
                //    Expression.Constant(commandText),
                //    Expression.Constant(parameters),
                //    paramSets
                //    );
                plan = Expression.Call(this.executor, "Batch", null, batchContext);
            }

            return(plan);
        }
コード例 #11
0
 internal static ProjectionExpression FindProjection(Expression expression)
 {
     var finder = new ProjectionFinder();
     finder.Visit(expression);
     return finder.found;
 }