private DslFormatBlock CreateFormatExprFromFields(IEnumerable <DataSourceFieldInfo> availableFields) =>
 new DslFormatBlock
 {
     Selects = availableFields.Select(_ => new DslFormatItem
     {
         Value = _queryVariableNameBuilder.Encode(_.Key)
     })
 };
        public string Translate([NotNull] DslGroupBlock queryBlock)
        {
            if (queryBlock == null)
            {
                throw new ArgumentNullException(nameof(queryBlock));
            }

            var properties = queryBlock.Items.Select(_ =>
                                                     _queryVariableNameBuilder.ToProperty(
                                                         _queryVariableNameBuilder.Encode(_.VariableName)));

            return($".GroupBy(x => new{{{string.Join(",", properties)}}})");
        }
        public string ToDsl([NotNull] DslOrderBlock queryBlock)
        {
            if (queryBlock == null)
            {
                throw new ArgumentNullException(nameof(queryBlock));
            }

            if (queryBlock.Items == null ||
                !queryBlock.Items.Any())
            {
                return(string.Empty);
            }

            var orderFieds = string.Join(", ",
                                         queryBlock.Items
                                         .Select(_ =>
                                                 $"{_queryVariableNameBuilder.Encode(_.OrderFieldName)} {(_.SortOrder == SortOrder.Ascending ? DslKeywords.Asc : DslKeywords.Desc)}"));

            return($"{DslKeywords.Order} {orderFieds}");
        }
        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();
        }