Ejemplo n.º 1
0
        public void Validate(DslDataQuery query, long?projectId, bool isSystem)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            if ((projectId == null) && isSystem)
            {
                return;
            }

            var tableDataSource = _dataSourceInfoProvider.Get(query.QueryEntityName, _userPrincipal.Info.Id);

            var isDataSourceFromTable = tableDataSource != null;

            if (!isDataSourceFromTable)
            {
                return;
            }

            if (!_dataSourceAccessValidator.CanReadSource(tableDataSource.Id, _userPrincipal.Info.Id))
            {
                throw new UnauthorizedAccessException();
            }
        }
        public IEnumerable <QueryResultColumn> Evaluate(DslDataQuery query, long userId)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var entityType = _queryEntityNameTranslator.GetEntityType(query.QueryEntityName);

            var dataSource = _dataSourceInfoProvider.Get(entityType.Name, userId);

            if (dataSource == null)
            {
                return(Enumerable.Empty <QueryResultColumn>());
            }

            var isQueryBlocksIsEmpty = (query.Blocks == null) ||
                                       !query.Blocks.Any();

            if (isQueryBlocksIsEmpty)
            {
                return(GetDefaultColumns(query, userId, dataSource));
            }

            RestrictFirstSelect(query, dataSource, userId);

            _queryProjectRestrictor.Restrict(query, entityType, dataSource, userId);

            return(GetLastSelectColumns(query));
        }
Ejemplo n.º 3
0
        public void ShouldRemoveEmptyFormatItems()
        {
            var formatBlock = new DslFormatBlock
            {
                Selects = new List <DslFormatItem>
                {
                    new DslFormatItem
                    {
                        Value = "Test"
                    },
                    new DslFormatItem
                    {
                        Value = string.Empty
                    },
                    new DslFormatItem
                    {
                        Value = null
                    }
                }
            };

            var sourceModel = new DslDataQuery
            {
                Blocks = new IDslQueryBlock[]
                {
                    formatBlock
                }
            };

            _target.Validate(sourceModel);

            formatBlock.Selects.Count().ShouldBeEquivalentTo(1);
        }
Ejemplo n.º 4
0
        public string ToDsl([NotNull] DslDataQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var stringBuilder = new StringBuilder();

            TranslateEntityName(query, stringBuilder);

            TranslateBlocks(query, stringBuilder);

            if (query.TakeFirst)
            {
                stringBuilder.AppendLine(DslKeywords.First);
            }

            if (query.TakeFirstOrDefault)
            {
                stringBuilder.AppendLine(DslKeywords.FirstOrDefault);
            }

            if (!string.IsNullOrEmpty(query.TableKey))
            {
                stringBuilder.AppendLine($"{DslKeywords.Table} {query.TableKey}");
            }

            return(stringBuilder.ToString());
        }
        public string RenderToTable([NotNull] DslDataQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            if (string.IsNullOrEmpty(query.TableKey) &&
                !query.IsTableRenderRequired)
            {
                return(string.Empty);
            }

            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine(".Select(x => new QueryResultItem {");

            if (!string.IsNullOrEmpty(query.TableKey))
            {
                stringBuilder.AppendLine("EntityId = x." + query.TableKey + ",");
            }

            stringBuilder.AppendLine("Value = x })");

            return(stringBuilder.ToString());
        }
        public void Validate([NotNull] DslDataQuery dslDataQuery)
        {
            if (dslDataQuery == null)
            {
                throw new ArgumentNullException(nameof(dslDataQuery));
            }

            RemoveEmptyFormatItems(dslDataQuery);
        }
Ejemplo n.º 7
0
        private object ExecuteExpr(DslDataQuery query)
        {
            var linqQuery  = _translator.Translate(query);
            var entityType = _nameTranslator.GetEntityType(query.QueryEntityName);
            var source     = _dataSourceProvider.GetDataSource(entityType);

            var result = _executor.Execute(source, entityType, linqQuery);

            return(result);
        }
        private static IEnumerable <QueryResultColumn> GetLastSelectColumns(DslDataQuery query)
        {
            var lastFormatBlock = query.Blocks.OfType <DslFormatBlock>().Last();

            return(lastFormatBlock.Selects.Select(_ => new QueryResultColumn
            {
                Code = _.Name,
                Name = _.DisplayName ?? _.Name,
                Description = _.Description
            }));
        }
 private static void ApplyTakeFirst(DslDataQuery queryExpression, StringBuilder builder)
 {
     if (queryExpression.TakeFirst)
     {
         builder.AppendLine(".First()");
     }
     else if (queryExpression.TakeFirstOrDefault)
     {
         builder.AppendLine(".FirstOrDefault()");
     }
 }
Ejemplo n.º 10
0
        public void ShouldConvertQueryToModel()
        {
            const string query = "query";

            var queryModel = new DslDataQuery();

            _dslParser.Setup(_ => _.DataQueryParse(query)).Returns(queryModel);

            var result = _target.ToQuery(query);

            result.ShouldBeEquivalentTo(queryModel);
        }
Ejemplo n.º 11
0
        public void ShouldSetializeDataQueryToJson()
        {
            var dataQuery = new DslDataQuery
            {
                Blocks = new IDslQueryBlock[]
                {
                    new DslGroupBlock()
                }
            };

            var result = dataQuery.ToJson();

            result.Should().NotBeNullOrEmpty();
        }
        private void RenderBlocks(DslDataQuery queryExpression, StringBuilder builder)
        {
            if (queryExpression.Blocks == null || !queryExpression.Blocks.Any())
            {
                return;
            }

            // ReSharper disable once LoopCanBePartlyConvertedToQuery
            foreach (var queryBlock in queryExpression.Blocks)
            {
                var translatedBlock = _queryBlockTranslationManager.Translate((dynamic)queryBlock);

                builder.AppendLine(translatedBlock);
            }
        }
Ejemplo n.º 13
0
        private void TranslateBlocks(DslDataQuery query, StringBuilder stringBuilder)
        {
            if (query.Blocks == null)
            {
                return;
            }

            // ReSharper disable once LoopCanBePartlyConvertedToQuery
            foreach (var queryBlock in query.Blocks)
            {
                var translatedBlock = _queryBlockTranslationManager.ToDsl((dynamic)queryBlock);

                stringBuilder.AppendLine(translatedBlock);
            }
        }
Ejemplo n.º 14
0
        public void ShouldConvertQueryFromDsl()
        {
            const string query = "query";

            const int projectId = 4;

            var queryModel = new DslDataQuery();

            _queryTranslator.Setup(_ => _.ToDsl(queryModel)).Returns(query);

            _queryModelAccessValidator.Setup(_ => _.Validate(queryModel, projectId, false));

            var result = _target.ToText(queryModel, projectId, false);

            result.ShouldBeEquivalentTo(query);
        }
        public string Translate(DslDataQuery queryExpression)
        {
            if (queryExpression == null)
            {
                throw new ArgumentNullException(nameof(queryExpression));
            }

            var builder = new StringBuilder("source.Query()\n");

            RenderBlocks(queryExpression, builder);

            builder.AppendLine(_queryToTableRenderer.RenderToTable(queryExpression));

            ApplyTakeFirst(queryExpression, builder);

            return(builder.ToString());
        }
Ejemplo n.º 16
0
        public void ShouldDeserializeDataQueryFromJson()
        {
            var dataQuery = new DslDataQuery
            {
                Blocks = new IDslQueryBlock[]
                {
                    new DslLimitBlock()
                },
                QueryEntityName = "testQuery"
            };

            var json = dataQuery.ToJson();

            var result = json.FromJson <DslDataQuery>();

            result.Should().NotBeNull();

            result.QueryEntityName.ShouldBeEquivalentTo(dataQuery.QueryEntityName);
        }
Ejemplo n.º 17
0
        public void ShouldProcessQuery()
        {
            const string textQuery = "query";

            const string entityName = "entityName";

            var query = new DslDataQuery
            {
                QueryEntityName = entityName
            };

            const string expr = "expr";

            var dataSource = new TestDataSource <object>();

            var resultObject = new object();

            _dslParser.Setup(_ => _.DataQueryParse(textQuery)).Returns(query);

            _dataQueryExpressionTranslator.Setup(_ => _.Translate(query)).Returns(expr);

            _queryEntityNameTranslator.Setup(_ => _.GetEntityType(entityName)).Returns(GetType);

            _dataSourceProvider.Setup(_ => _.GetDataSource(GetType())).Returns(dataSource);

            _dataQueryExecutor.Setup(_ => _.Execute(dataSource, GetType(), expr)).Returns(resultObject);

            var result = _target.Execute(textQuery);

            _dslParser.Verify(_ => _.DataQueryParse(textQuery), Times.Once);

            _dataQueryExpressionTranslator.Verify(_ => _.Translate(query), Times.Once);

            _queryEntityNameTranslator.Verify(_ => _.GetEntityType(entityName), Times.Once);

            _dataSourceProvider.Verify(_ => _.GetDataSource(GetType()), Times.Once);

            _dataQueryExecutor.Verify(_ => _.Execute(dataSource, GetType(), expr), Times.Once);

            result.ShouldBeEquivalentTo(resultObject);
        }
Ejemplo n.º 18
0
        public void ShouldConvertQueryToDsl()
        {
            var queryModel = new DslDataQuery
            {
                Blocks = new []
                {
                    new DslLimitBlock
                    {
                        Skip = 1,
                        Take = 2
                    }
                },
                QueryEntityName    = "entityName",
                TableKey           = "table",
                TakeFirst          = true,
                TakeFirstOrDefault = true
            };

            var result = _target.ToDsl(queryModel);

            result.Should().NotBeNullOrEmpty();
        }
Ejemplo n.º 19
0
        public void ShouldNotThrowExceptionWhenHaventAccessToDataSource()
        {
            const string entityName = "entity";

            var query = new DslDataQuery
            {
                QueryEntityName = entityName
            };

            const long userId = 523243;

            var userInfo = new UserInfo
            {
                Id = userId
            };

            _userPrincipal.Setup(_ => _.Info).Returns(userInfo);

            const long dataSourceId = 23443234;

            var dataSource = new DataSourceInfo
            {
                Id = dataSourceId
            };

            long?projectId = 52123123;

            _dataSourceInfoProvider
            .Setup(_ => _.Get(entityName, userId))
            .Returns(dataSource);

            _dataSourceAccessValidator
            .Setup(_ => _.CanReadSource(dataSourceId, userId))
            .Returns(false);

            Assert.Throws <UnauthorizedAccessException>(() => _target.Validate(query, projectId, false));
        }
Ejemplo n.º 20
0
        public void ShouldNotThrowExceptionWhenDataSourceDoesNotExists()
        {
            const string entityName = "entity";

            var query = new DslDataQuery
            {
                QueryEntityName = entityName
            };

            const long userId = 523243;

            long?projectId = 423434;

            var userInfo = new UserInfo
            {
                Id = userId
            };

            _userPrincipal.Setup(_ => _.Info).Returns(userInfo);

            _dataSourceInfoProvider.Setup(_ => _.Get(entityName, userId)).Returns((DataSourceInfo)null);

            _target.Validate(query, projectId, false);
        }
        private IEnumerable <QueryResultColumn> GetDefaultColumns([NotNull] DslDataQuery query,
                                                                  long userId,
                                                                  DataSourceInfo dataSource)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var availableFields = _dataSourceFieldInfoProvider.GetBySource(dataSource.Id, userId);

            var dslFormatBlock = CreateFormatExprFromFields(availableFields);

            query.Blocks = new IDslQueryBlock[] { dslFormatBlock };

            var columns = availableFields.Select(_ => new QueryResultColumn
            {
                Code        = _.Key,
                Description = _.Description,
                Name        = _.Name
            });

            return(columns);
        }
        private static void RemoveEmptyFormatItems(DslDataQuery dslDataQuery)
        {
            foreach (var queryBlock in dslDataQuery.Blocks)
            {
                // ReSharper disable once InvertIf
                if (queryBlock is DslFormatBlock)
                {
                    var formatBlock = queryBlock as DslFormatBlock;

                    var selectItemsList = new List <DslFormatItem>();

                    // ReSharper disable once LoopCanBeConvertedToQuery
                    foreach (var selectItem in formatBlock.Selects)
                    {
                        if (!string.IsNullOrEmpty(selectItem.Value))
                        {
                            selectItemsList.Add(selectItem);
                        }
                    }

                    formatBlock.Selects = selectItemsList.ToArray();
                }
            }
        }
        private void RestrictFirstSelect(DslDataQuery query, DataSourceInfo dataSource, long userId)
        {
            var firstFormatBlock = GetFirstFormatBlock(query);

            ProcessSelects(dataSource, userId, firstFormatBlock);
        }
Ejemplo n.º 24
0
        public void Update(
            long queryId,
            DslDataQuery dslQuery,
            string name,
            string comment,
            QueryPrivacyType privacyType,
            QueryVisibilityType visibilityType)
        {
            using (var telemetryScope = _telemetryScopeProvider.Create <Queries>(TelemetryOperationNames.Query.Update))
            {
                try
                {
                    if (string.IsNullOrEmpty(name))
                    {
                        throw new ArgumentNullException(nameof(name));
                    }

                    var query = _queryRepository.GetById(queryId);

                    if (query == null)
                    {
                        throw new QueryDoesNotExistsException(queryId);
                    }

                    telemetryScope.SetEntity(query);

                    var canEditQuery = _queryAccessValidator.IsCanEdit(query, _userPrincipal.Info.Id);

                    if (!canEditQuery)
                    {
                        throw new UnauthorizedAccessException();
                    }

                    string dslQueryText = null;

                    if (dslQuery != null)
                    {
                        dslQueryText = _queryModelProcessor.ToText(dslQuery, query.ProjectId, query.IsSystem);
                    }

                    _queryModelValidator.Validate(dslQuery);

                    query.Comment     = comment;
                    query.JsonQuery   = dslQuery.ToJson();
                    query.ModifiedUtc = _timeService.GetUtc();
                    query.Name        = name;
                    query.Privacy     = (int)privacyType;
                    query.Query       = dslQueryText;
                    query.Visibility  = (int)visibilityType;

                    _queryRepository.Save();

                    telemetryScope.WriteSuccess();
                }
                catch (Exception ex)
                {
                    telemetryScope.WriteException(ex);

                    throw;
                }
            }
        }
 private static DslFormatBlock GetFirstFormatBlock(DslDataQuery query) =>
 query.Blocks.OfType <DslFormatBlock>().First();
        public void Restrict([NotNull] DslDataQuery query,
                             [NotNull] Type entityType,
                             [NotNull] DataSourceInfo dataSource,
                             long userId)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (entityType == null)
            {
                throw new ArgumentNullException(nameof(entityType));
            }
            if (dataSource == null)
            {
                throw new ArgumentNullException(nameof(dataSource));
            }

            var projectProperty = entityType
                                  .GetCustomAttributes(typeof(ProjectPropertyAttribute), false)
                                  .Cast <ProjectPropertyAttribute>()
                                  .SingleOrDefault();

            if (projectProperty == null)
            {
                return;
            }

            var userProjectIds = _dataSourceAccessValidator
                                 .GetDataSourceProjects(dataSource.Key, userId)
                                 .ToArray();

            if (!userProjectIds.Any())
            {
                throw new UnauthorizedAccessException();
            }

            var blocks = new List <IDslQueryBlock>();

            var propertyName = _queryVariableNameBuilder.Encode(projectProperty.PropertyName);

            var restrictWhere = new DslFilterBlock
            {
                Specification = new FilterSpecification
                {
                    LeftSpecification = new FilterArraySpecification
                    {
                        Specifications = userProjectIds.Select(_ =>
                                                               new FilterConstantSpecification
                        {
                            Value = $"{_}L"
                        })
                    },
                    Operator           = FilterOperator.Contains,
                    RightSpecification = new FilterParameterSpecification
                    {
                        Value = propertyName
                    }
                }
            };

            blocks.Add(restrictWhere);

            blocks.AddRange(query.Blocks);

            query.Blocks = blocks.ToArray();
        }
Ejemplo n.º 27
0
        public IEnumerable <QueryResultItem> ExecuteTable(DslDataQuery query)
        {
            query.IsTableRenderRequired = true;

            return(ExecuteExpr(query) as IEnumerable <QueryResultItem>);
        }
Ejemplo n.º 28
0
 private static void TranslateEntityName(DslDataQuery query, StringBuilder stringBuilder)
 => stringBuilder.AppendLine(query.QueryEntityName);
        public string ToText(DslDataQuery query, long?projectId, bool isSystem)
        {
            _queryModelAccessValidator.Validate(query, projectId, isSystem);

            return(_queryTranslator.ToDsl(query));
        }