From 0002cec8b4740f12ec5483a6be5f780fffebc88a Mon Sep 17 00:00:00 2001 From: KaneLeung Date: Tue, 30 Mar 2021 11:09:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?:rocket:=E5=A2=9E=E5=8A=A0=E5=88=86?= =?UTF-8?q?=E9=A1=B5=E6=A8=A1=E5=9E=8B=E5=88=86=E9=A1=B5=E5=8F=8A=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/SongController.cs | 11 ++++ Examples/repository_01/PagingInfo.cs | 57 +++++++++++++++++++ .../restful/Controllers/SongController.cs | 12 ++++ Examples/restful/PagingInfo.cs | 57 +++++++++++++++++++ FreeSql/FreeSql.xml | 27 +++++++++ FreeSql/Interface/Curd/ISelect/ISelect0.cs | 7 +++ .../SelectProvider/Select0Provider.cs | 7 +++ FreeSql/Internal/Model/BasePagingInfo.cs | 25 ++++++++ 8 files changed, 203 insertions(+) create mode 100644 Examples/repository_01/PagingInfo.cs create mode 100644 Examples/restful/PagingInfo.cs create mode 100644 FreeSql/Internal/Model/BasePagingInfo.cs diff --git a/Examples/repository_01/Controllers/SongController.cs b/Examples/repository_01/Controllers/SongController.cs index 4381d575..a6bb50de 100644 --- a/Examples/repository_01/Controllers/SongController.cs +++ b/Examples/repository_01/Controllers/SongController.cs @@ -63,6 +63,17 @@ namespace restful.Controllers return _songRepository.Select.WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(page, limit).ToListAsync(); } + /// + /// curl -X GET "http://localhost:5000/restapi/Songs/GetPagingItems?key=FreeSql&PageNumber=2&PageSize=10" -H "accept: text/plain" + /// + /// + /// + [HttpGet("GetPagingItems")] + public Task> GetPagingItems([FromQuery] string key, [FromQuery] PagingInfo pagingInfo) + { + return _songRepository.Select.WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(pagingInfo).ToListAsync(); + } + [HttpGet("{id}")] public Task GetItem([FromRoute] int id) { diff --git a/Examples/repository_01/PagingInfo.cs b/Examples/repository_01/PagingInfo.cs new file mode 100644 index 00000000..803ec20c --- /dev/null +++ b/Examples/repository_01/PagingInfo.cs @@ -0,0 +1,57 @@ +using FreeSql.Internal.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace repository_01 +{ + public class PagingInfo : BasePagingInfo + { + /// + /// 无参构造函数 + /// + public PagingInfo() + { + } + /// + /// 当前为第1页,每页大小的构造函数 + /// + /// + public PagingInfo(int pageSize) + { + PageNumber = 1; + PageSize = pageSize; + } + /// + /// 带当前页和每页大小的构造函数 + /// + /// + /// + public PagingInfo(int pageNumber, int pageSize) + { + PageNumber = pageNumber; + PageSize = pageSize; + } + /// + /// 当前有多少页【只读】 + /// + public long PageCount => PageSize == 0 ? 0 : (Count + PageSize - 1) / PageSize; + /// + /// 是否有上一页【只读】 + /// + public bool HasPrevious => PageNumber > 1 && PageNumber <= PageCount; + /// + /// 是否有下一页【只读】 + /// + public bool HasNext => PageNumber < PageCount; + /// + /// 是否在第一页【只读】 + /// + public bool IsFrist => PageNumber == 1; + /// + /// 是否在最后一页【只读】 + /// + public bool IsLast => PageNumber == PageCount; + } +} \ No newline at end of file diff --git a/Examples/restful/Controllers/SongController.cs b/Examples/restful/Controllers/SongController.cs index 0cd631ae..98980779 100644 --- a/Examples/restful/Controllers/SongController.cs +++ b/Examples/restful/Controllers/SongController.cs @@ -25,6 +25,18 @@ namespace restful.Controllers return _fsql.Select().WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(page, limit).ToListAsync(); } + /// + /// curl -X GET "http://localhost:5000/restapi/Songs/GetPagingItems?key=FreeSql&PageNumber=2&PageSize=10" -H "accept: text/plain" + /// + /// + /// + /// + [HttpGet("GetPagingItems")] + public Task> GetPagingItems([FromQuery] string key, [FromQuery] PagingInfo pagingInfo) + { + return _fsql.Select().WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(pagingInfo).ToListAsync(); + } + [HttpGet("{id}")] public Task GetItem([FromRoute] int id) { diff --git a/Examples/restful/PagingInfo.cs b/Examples/restful/PagingInfo.cs new file mode 100644 index 00000000..ab52c180 --- /dev/null +++ b/Examples/restful/PagingInfo.cs @@ -0,0 +1,57 @@ +using FreeSql.Internal.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace restful +{ + public class PagingInfo : BasePagingInfo + { + /// + /// 无参构造函数 + /// + public PagingInfo() + { + } + /// + /// 当前为第1页,每页大小的构造函数 + /// + /// + public PagingInfo(int pageSize) + { + PageNumber = 1; + PageSize = pageSize; + } + /// + /// 带当前页和每页大小的构造函数 + /// + /// + /// + public PagingInfo(int pageNumber, int pageSize) + { + PageNumber = pageNumber; + PageSize = pageSize; + } + /// + /// 当前有多少页【只读】 + /// + public long PageCount => PageSize == 0 ? 0 : (Count + PageSize - 1) / PageSize; + /// + /// 是否有上一页【只读】 + /// + public bool HasPrevious => PageNumber > 1 && PageNumber <= PageCount; + /// + /// 是否有下一页【只读】 + /// + public bool HasNext => PageNumber < PageCount; + /// + /// 是否在第一页【只读】 + /// + public bool IsFrist => PageNumber == 1; + /// + /// 是否在最后一页【只读】 + /// + public bool IsLast => PageNumber == PageCount; + } +} \ No newline at end of file diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index ad59871a..6d7acdfc 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -2136,6 +2136,13 @@ 每页多少 + + + 分页 + + 分页信息 + + 查询数据前,去重 @@ -3976,6 +3983,26 @@ + + + 分页信息 + + + + + 第几页,从1开始 + + + + + 每页多少 + + + + + 查询的记录数量 + + 当前操作的数据 diff --git a/FreeSql/Interface/Curd/ISelect/ISelect0.cs b/FreeSql/Interface/Curd/ISelect/ISelect0.cs index 1ecab564..d17490da 100644 --- a/FreeSql/Interface/Curd/ISelect/ISelect0.cs +++ b/FreeSql/Interface/Curd/ISelect/ISelect0.cs @@ -402,6 +402,13 @@ namespace FreeSql /// TSelect Page(int pageNumber, int pageSize); + /// + /// 分页 + /// + /// 分页信息 + /// + TSelect Page(BasePagingInfo pagingInfo); + /// /// 查询数据前,去重 /// diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs index 74cfd9f3..d82980eb 100644 --- a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs +++ b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs @@ -319,6 +319,13 @@ namespace FreeSql.Internal.CommonProvider return this.Limit(pageSize) as TSelect; } + public TSelect Page(BasePagingInfo pagingInfo) + { + pagingInfo.Count = this.Count(); + this.Skip(Math.Max(0, pagingInfo.PageNumber - 1) * pagingInfo.PageSize); + return this.Limit(pagingInfo.PageSize) as TSelect; + } + public TSelect Skip(int offset) { _skip = offset; diff --git a/FreeSql/Internal/Model/BasePagingInfo.cs b/FreeSql/Internal/Model/BasePagingInfo.cs new file mode 100644 index 00000000..2a48e529 --- /dev/null +++ b/FreeSql/Internal/Model/BasePagingInfo.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace FreeSql.Internal.Model +{ + /// + /// 分页信息 + /// + public class BasePagingInfo + { + /// + /// 第几页,从1开始 + /// + public int PageNumber { get; set; } + /// + /// 每页多少 + /// + public int PageSize { get; set; } + /// + /// 查询的记录数量 + /// + public long Count { get; set; } + } +} \ No newline at end of file From f2a22aa8958ccf7c14fdbff5e3ac519f148becfc Mon Sep 17 00:00:00 2001 From: KaneLeung Date: Tue, 30 Mar 2021 11:45:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?:rocket:=E8=A1=A5=E5=85=85ISelectGrouping?= =?UTF-8?q?=E7=9A=84=E5=88=86=E9=A1=B5=E6=A8=A1=E5=9E=8B=E5=88=86=E9=A1=B5?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/FreeSql.xml | 7 +++++++ FreeSql/Interface/Curd/ISelect/ISelectGrouping.cs | 10 +++++++++- .../SelectProvider/SelectGroupingProvider.cs | 8 ++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 6d7acdfc..892888fe 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -2614,6 +2614,13 @@ 每页多少 + + + 分页 + + 分页信息 + + 查询的记录数量 diff --git a/FreeSql/Interface/Curd/ISelect/ISelectGrouping.cs b/FreeSql/Interface/Curd/ISelect/ISelectGrouping.cs index 295b7411..c787cc61 100644 --- a/FreeSql/Interface/Curd/ISelect/ISelectGrouping.cs +++ b/FreeSql/Interface/Curd/ISelect/ISelectGrouping.cs @@ -1,4 +1,5 @@ -using System; +using FreeSql.Internal.Model; +using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Text; @@ -101,6 +102,13 @@ namespace FreeSql /// ISelectGrouping Page(int pageNumber, int pageSize); + /// + /// 分页 + /// + /// 分页信息 + /// + ISelectGrouping Page(BasePagingInfo pagingInfo); + /// /// 查询的记录数量 /// diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/SelectGroupingProvider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/SelectGroupingProvider.cs index 09c524e0..68ca4c50 100644 --- a/FreeSql/Internal/CommonProvider/SelectProvider/SelectGroupingProvider.cs +++ b/FreeSql/Internal/CommonProvider/SelectProvider/SelectGroupingProvider.cs @@ -201,6 +201,14 @@ namespace FreeSql.Internal.CommonProvider return this; } + public ISelectGrouping Page(BasePagingInfo pagingInfo) + { + pagingInfo.Count = this.Count(); + _groupBySkip = Math.Max(0, pagingInfo.PageNumber - 1) * pagingInfo.PageSize; + _groupByLimit = pagingInfo.PageSize; + return this; + } + public long Count() => _select._cancel?.Invoke() == true ? 0 : long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_select._connection, _select._transaction, CommandType.Text, $"select count(1) from ({this.ToSql($"1{_comonExp._common.FieldAsAlias("as1")}")}) fta", _select._commandTimeout, _select._params.ToArray())), out var trylng) ? trylng : default(long); public ISelectGrouping Count(out long count) {