Example #1
0
        private void GenerateGrouping(SelectStatement statement, Grouping grouping)
        {
            if (grouping == null)
            {
                return;
            }

            if (grouping.Keys != null && grouping.Keys.Length > 0)
            {
                //创建分组子句
                statement.GroupBy = new GroupByClause();

                foreach (var key in grouping.Keys)
                {
                    var source = statement.From(key.Name, null, out var property);

                    if (property.IsComplex)
                    {
                        throw new DataException($"The grouping key '{property.Name}' can not be a complex property.");
                    }

                    statement.GroupBy.Keys.Add(source.CreateField(property));
                    statement.Select.Members.Add(source.CreateField(property.GetFieldName(out var alias), key.Alias ?? alias));
                }

                if (grouping.Filter != null)
                {
                    statement.GroupBy.Having = statement.Where(grouping.Filter);
                }
            }

            foreach (var aggregate in grouping.Aggregates)
            {
                if (string.IsNullOrEmpty(aggregate.Name) || aggregate.Name == "*")
                {
                    statement.Select.Members.Add(
                        new AggregateExpression(aggregate.Function, Expression.Constant(0))
                    {
                        Alias = string.IsNullOrEmpty(aggregate.Alias) ? aggregate.Function.ToString() : aggregate.Alias
                    });
                }
                else
                {
                    var source = statement.From(aggregate.Name, null, out var property);

                    if (property.IsComplex)
                    {
                        throw new DataException($"The field '{property.Name}' of aggregate function can not be a complex property.");
                    }

                    statement.Select.Members.Add(
                        new AggregateExpression(aggregate.Function, source.CreateField(property))
                    {
                        Alias = string.IsNullOrEmpty(aggregate.Alias) ? aggregate.Name : aggregate.Alias
                    });
                }
            }
        }
Example #2
0
        private void GenerateSortings(SelectStatement statement, TableIdentifier origin, Sorting[] sortings)
        {
            if (sortings == null || sortings.Length == 0)
            {
                return;
            }

            statement.OrderBy = new OrderByClause();

            foreach (var sorting in sortings)
            {
                if (string.IsNullOrEmpty(sorting.Name))
                {
                    continue;
                }

                var source = statement.From(origin, sorting.Name, null, out var property);

                var simplex = property.IsSimplex ?
                              (IDataEntitySimplexProperty)property :
                              throw new DataException($"The specified '{property.Entity.Name}.{property.Name}' is a composite(navigation) property that is not sortable.");

                if (simplex.IsPrimaryKey || simplex.Sortable)
                {
                    statement.OrderBy.Add(source.CreateField(property), sorting.Mode);
                }
                else
                {
                    throw new DataException($"The specified '{property.Entity.Name}.{property.Name}' property is not sortable and must be enabled for sorting before it can be sorted.");
                }
            }
        }
        private void GenerateSortings(SelectStatement statement, TableIdentifier origin, Sorting[] sortings)
        {
            if (sortings == null || sortings.Length == 0)
            {
                return;
            }

            statement.OrderBy = new OrderByClause();

            foreach (var sorting in sortings)
            {
                var source = statement.From(origin, sorting.Name, out var property);
                statement.OrderBy.Add(source.CreateField(property), sorting.Mode);
            }
        }