Пример #1
0
        public QBuilder PageBy <TField>(Expression <Func <TTable, TField> > fieldNameDescriber, uint page, ushort pageSize, bool orderAscending = true)
        {
            var fieldName   = new FieldNameResolver().GetFieldName(fieldNameDescriber);
            var tableName   = QBuilder.TableNameAliaser.GetTableAlias <TTable>();
            var range       = PageRangeCalculator.GetPageRange(0, page, pageSize);
            var orderClause = $"Order By `{fieldName}`";

            if (orderAscending)
            {
                orderClause += " Asc";
            }
            else
            {
                orderClause += " Desc";
            }
            QBuilder.SetSuffix($" {orderClause} Limit {range.Start},{range.PageSize}");
            return(QBuilder);
        }
        public QBuilder PageBy <TField>(Expression <Func <TTable, TField> > fieldNameDescriber, uint page, ushort pageSize, bool orderAscending = true)
        {
            new DataValidator()
            .AddFailureCondition(page < 1, $"Database query requested for page '{page}'. Pages must be greater than or equal to 1", false)
            .AddFailureCondition(pageSize < 1, $"Pages must have at least one record. Page size '{pageSize}' is not valid", false)
            .ThrowExceptionOnInvalidRules();
            const string rowNumber   = "RowNumber";
            string       orderSuffix = orderAscending ? "Asc" : "Desc";
            var          fieldName   = new FieldNameResolver().GetFieldName(fieldNameDescriber);
            var          table       = QBuilder.TableNameAliaser.GetTableAlias <TTable>();
            var          range       = PageRangeCalculator.GetPageRange(AbsoluteFirstRecordIndex, page, pageSize);

            QBuilder.UseSelector()
            .SetSelectPrefix($"ROW_NUMBER() OVER (ORDER BY [{table}].[{fieldName}] {orderSuffix}) AS {rowNumber},")
            .Then()
            .UseFilter();

            QBuilder.SetSuffix($"Where {rowNumber} >= {range.Start} AND {rowNumber} <= {range.End}");
            return(QBuilder);
        }