public ExecutorResult Execute()
        {
            return(RunDiagnosis.Watch(() =>
            {
                Check.IfNullOrZero(_model);

                var instance = _model;
                instance.SetAddTime();
                instance.OnChanged();

                if (_options.ModelValidate)
                {
                    instance.CheckPropertyValue();
                }
                var(tableName, aliasName) = instance.GetEntityBaseAliasName();

                var sqlElements = instance.GetChangedProperties().GetSqlElements();
                var insert = _templateBase.CreateInsert(tableName, sqlElements.Fields, sqlElements.InsertPlaceHolders);

                var statementResultBuilder = new StatementResultBuilder();
                statementResultBuilder.AddStatementTemplate(insert);

                foreach (var item in sqlElements.Parameters)
                {
                    statementResultBuilder.AddParameter(item);
                }
                return _resultExecutor.Execute(statementResultBuilder);
            }));
        }
        public ExecutorResult Execute()
        {
            return(RunDiagnosis.Watch(() =>
            {
                if (!FromComponent.AliasNameMappers.Any())
                {
                    throw new ArgumentException("From");
                }

                var mainTable = FromComponent.AliasNameMappers[0];
                var selectStatement = _options.TemplateBase.CreateSelect(ExtractSelectFields(), mainTable.Key, mainTable.Value);
                var statementResultBuilder = Translate(selectStatement, WhereComponent, FromComponent, JoinComponents);
                JoinComponents.Clear();

                if (PaginationComponent != null)
                {
                    if (OrderComponent == null)
                    {
                        throw new Exception("Order");
                    }
                    var(fields, tableName) = ExtractOrderFields();
                    var orderTemplate = _options.TemplateBase.CreateOrderBy(OrderComponent.OrderBy, $@"{tableName}.{fields}");
                    _options.TemplateBase.CreatePagination(PaginationComponent, orderTemplate, statementResultBuilder.StatmentTemplate);
                }
                else if (OrderComponent != null)
                {
                    var(fields, tableName) = ExtractOrderFields();
                    var orderTemplate = _options.TemplateBase.CreateOrderBy(OrderComponent.OrderBy, $@"{tableName}.{fields}");
                    selectStatement.Append(orderTemplate);
                }

                return _resultExecutor.Execute(statementResultBuilder);
            }));
        }
Exemple #3
0
        public void ExecuteThrowsExceptionWhenResponseStartedTest()
        {
            var mvcOptions    = new MvcOptions();
            var options       = Substitute.For <IOptions <MvcOptions> >();
            var writerFactory = Substitute.For <IHttpResponseStreamWriterFactory>();
            var loggerFactory = Substitute.For <ILoggerFactory>();

            options.Value.Returns(mvcOptions);

            var formatSelector = new DefaultOutputFormatterSelector(options, loggerFactory);

            var executor = new ObjectResultExecutor(formatSelector, writerFactory, loggerFactory);
            var result   = new ObjectResult(Guid.NewGuid().ToString());
            var context  = Substitute.For <HttpContext>();
            var response = Substitute.For <HttpResponse>();

            response.HasStarted.Returns(true);

            context.Response.Returns(response);

            var sut = new ResultExecutor(executor);

            Func <Task> action = async() => await sut.Execute(context, result).ConfigureAwait(false);

            action.Should().Throw <InvalidOperationException>();
        }
        public ExecutorResult Execute()
        {
            return(RunDiagnosis.Watch(() =>
            {
                var instance = Model;
                instance.SetUpdateTime();

                if (_options.ModelValidate)
                {
                    instance.CheckPropertyValue();
                }

                var(tableName, aliasName) = instance.GetEntityBaseAliasName();
                var sqlElements = instance.GetChangedProperties().GetSqlElements();
                var update = _options.TemplateBase.CreateUpdate(tableName, aliasName, sqlElements.UpdatePlaceHolders);
                var statementResultBuilder = Translate(update, WhereComponent);
                instance.Reset();

                return _resultExecutor.Execute(statementResultBuilder);
            }));
        }
Exemple #5
0
        public void ExecuteThrowsExceptionWithNullContextTest()
        {
            var mvcOptions = new MvcOptions();
            var options    = Substitute.For <IOptions <MvcOptions> >();

            options.Value.Returns(mvcOptions);

            var writerFactory = Substitute.For <IHttpResponseStreamWriterFactory>();
            var loggerFactory = Substitute.For <ILoggerFactory>();

            var formatSelector = new DefaultOutputFormatterSelector(options, loggerFactory);

            var executor = new ObjectResultExecutor(formatSelector, writerFactory, loggerFactory);
            var result   = new ObjectResult(Guid.NewGuid().ToString());

            var sut = new ResultExecutor(executor);

            Func <Task> action = async() => await sut.Execute(null, result).ConfigureAwait(false);

            action.Should().Throw <ArgumentNullException>();
        }
Exemple #6
0
        public async Task ExecuteSendsResultToExecutorTest()
        {
            const HttpStatusCode StatusCode = HttpStatusCode.OK;
            var mvcOptions = new MvcOptions();
            var options    = Substitute.For <IOptions <MvcOptions> >();

            mvcOptions.OutputFormatters.Add(new StringOutputFormatter());
            options.Value.Returns(mvcOptions);

            var writerFactory = Substitute.For <IHttpResponseStreamWriterFactory>();
            var loggerFactory = Substitute.For <ILoggerFactory>();

            var formatSelector = new DefaultOutputFormatterSelector(options, loggerFactory);

            var executor = Substitute.For <ObjectResultExecutor>(formatSelector, writerFactory, loggerFactory);
            var result   = new ObjectResult(Guid.NewGuid().ToString())
            {
                StatusCode = (int)StatusCode
            };
            var context  = Substitute.For <HttpContext>();
            var response = Substitute.For <HttpResponse>();
            var headers  = new HeaderDictionary();
            var body     = Substitute.For <Stream>();

            response.HasStarted.Returns(false);
            response.HttpContext.Returns(context);
            response.Headers.Returns(headers);
            response.Body.Returns(body);
            context.Response.Returns(response);

            var sut = new ResultExecutor(executor);

            await sut.Execute(context, result).ConfigureAwait(false);

            response.StatusCode.Should().Be((int)StatusCode);
            await executor.Received().ExecuteAsync(
                Arg.Is <ActionContext>(x => x.HttpContext == context),
                Arg.Is <ObjectResult>(x => x == result)).ConfigureAwait(false);
        }