csharp/0x1000000/SqExpress/SqExpress/ModelRequestExtensions.cs

ModelRequestExtensions.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SqExpress.DataAccess;
using SqExpress.ModelSelect;
using SqExpress.Syntax.Select;

namespace SqExpress
{
    public static clast ModelRequestExtensions
    {
        public static Task QueryList(this ModelRequestData modelRequestData, ISqDatabase database)
            => modelRequestData.Expr.QueryList(database, modelRequestData.Mapper);

        public static Task Query(this ModelRequestData modelRequestData, ISqDatabase database, Action handler)
            => modelRequestData.Expr.Query(database, r=> handler(modelRequestData.Mapper(r)));

        public static Task Query(this ModelRequestData modelRequestData, ISqDatabase database, TAcc seed, Func handler)
            => modelRequestData.Expr.Query(database, seed, (acc, r)=> handler(acc, modelRequestData.Mapper(r)));

        public static Task QueryPage(this ModelRangeRequestData modelRequestData, ISqDatabase database)
            => modelRequestData.Expr.QueryPage(database, modelRequestData.Mapper);

        internal static async Task QueryPage(this ExprSelectOffsetFetch query, ISqDatabase database, Func reader)
        {
            var countColumn = CustomColumnFactory.Int32("$count");

            var selectQuery = (ExprQuerySpecification)query.SelectQuery;

            query = query.WithSelectQuery(
                selectQuery.WithSelectList(selectQuery.SelectList.Concat(SqQueryBuilder.CountOneOver().As(countColumn))));


            var res = await query.Query(database,
                new KeyValuePair(new List(), null),
                (acc, r) =>
                {
                    acc.Key.Add(reader(r));
                    var total = acc.Value ?? countColumn.Read(r);
                    return new KeyValuePair(acc.Key, total);
                });

            return new DataPage(res.Key, query.OrderBy.OffsetFetch.Offset.Value ?? 0, res.Value ?? 0);
        }
    }
}