Merge branch 'master' of github.com:dotnetcore/FreeSql

This commit is contained in:
luoyunchong 2021-04-10 20:28:16 +08:00
commit 28559066d3
11 changed files with 228 additions and 2 deletions

View File

@ -63,6 +63,17 @@ namespace restful.Controllers
return _songRepository.Select.WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(page, limit).ToListAsync(); return _songRepository.Select.WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(page, limit).ToListAsync();
} }
/// <summary>
/// curl -X GET "http://localhost:5000/restapi/Songs/GetPagingItems?key=FreeSql&PageNumber=2&PageSize=10" -H "accept: text/plain"
/// </summary>
/// <param name="pagingInfo"></param>
/// <returns></returns>
[HttpGet("GetPagingItems")]
public Task<List<Song>> GetPagingItems([FromQuery] string key, [FromQuery] PagingInfo pagingInfo)
{
return _songRepository.Select.WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(pagingInfo).ToListAsync();
}
[HttpGet("{id}")] [HttpGet("{id}")]
public Task<Song> GetItem([FromRoute] int id) public Task<Song> GetItem([FromRoute] int id)
{ {

View File

@ -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
{
/// <summary>
/// 无参构造函数
/// </summary>
public PagingInfo()
{
}
/// <summary>
/// 当前为第1页每页大小的构造函数
/// </summary>
/// <param name="pageSize"></param>
public PagingInfo(int pageSize)
{
PageNumber = 1;
PageSize = pageSize;
}
/// <summary>
/// 带当前页和每页大小的构造函数
/// </summary>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
public PagingInfo(int pageNumber, int pageSize)
{
PageNumber = pageNumber;
PageSize = pageSize;
}
/// <summary>
/// 当前有多少页【只读】
/// </summary>
public long PageCount => PageSize == 0 ? 0 : (Count + PageSize - 1) / PageSize;
/// <summary>
/// 是否有上一页【只读】
/// </summary>
public bool HasPrevious => PageNumber > 1 && PageNumber <= PageCount;
/// <summary>
/// 是否有下一页【只读】
/// </summary>
public bool HasNext => PageNumber < PageCount;
/// <summary>
/// 是否在第一页【只读】
/// </summary>
public bool IsFrist => PageNumber == 1;
/// <summary>
/// 是否在最后一页【只读】
/// </summary>
public bool IsLast => PageNumber == PageCount;
}
}

View File

@ -25,6 +25,18 @@ namespace restful.Controllers
return _fsql.Select<Song>().WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(page, limit).ToListAsync(); return _fsql.Select<Song>().WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(page, limit).ToListAsync();
} }
/// <summary>
/// curl -X GET "http://localhost:5000/restapi/Songs/GetPagingItems?key=FreeSql&PageNumber=2&PageSize=10" -H "accept: text/plain"
/// </summary>
/// <param name="key"></param>
/// <param name="pagingInfo"></param>
/// <returns></returns>
[HttpGet("GetPagingItems")]
public Task<List<Song>> GetPagingItems([FromQuery] string key, [FromQuery] PagingInfo pagingInfo)
{
return _fsql.Select<Song>().WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(pagingInfo).ToListAsync();
}
[HttpGet("{id}")] [HttpGet("{id}")]
public Task<Song> GetItem([FromRoute] int id) public Task<Song> GetItem([FromRoute] int id)
{ {

View File

@ -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
{
/// <summary>
/// 无参构造函数
/// </summary>
public PagingInfo()
{
}
/// <summary>
/// 当前为第1页每页大小的构造函数
/// </summary>
/// <param name="pageSize"></param>
public PagingInfo(int pageSize)
{
PageNumber = 1;
PageSize = pageSize;
}
/// <summary>
/// 带当前页和每页大小的构造函数
/// </summary>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
public PagingInfo(int pageNumber, int pageSize)
{
PageNumber = pageNumber;
PageSize = pageSize;
}
/// <summary>
/// 当前有多少页【只读】
/// </summary>
public long PageCount => PageSize == 0 ? 0 : (Count + PageSize - 1) / PageSize;
/// <summary>
/// 是否有上一页【只读】
/// </summary>
public bool HasPrevious => PageNumber > 1 && PageNumber <= PageCount;
/// <summary>
/// 是否有下一页【只读】
/// </summary>
public bool HasNext => PageNumber < PageCount;
/// <summary>
/// 是否在第一页【只读】
/// </summary>
public bool IsFrist => PageNumber == 1;
/// <summary>
/// 是否在最后一页【只读】
/// </summary>
public bool IsLast => PageNumber == PageCount;
}
}

View File

@ -2136,6 +2136,13 @@
<param name="pageSize">每页多少</param> <param name="pageSize">每页多少</param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSql.ISelect0`2.Page(FreeSql.Internal.Model.BasePagingInfo)">
<summary>
分页
</summary>
<param name="pagingInfo">分页信息</param>
<returns></returns>
</member>
<member name="M:FreeSql.ISelect0`2.Distinct"> <member name="M:FreeSql.ISelect0`2.Distinct">
<summary> <summary>
查询数据前,去重 查询数据前,去重
@ -2607,6 +2614,13 @@
<param name="pageSize">每页多少</param> <param name="pageSize">每页多少</param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSql.ISelectGrouping`2.Page(FreeSql.Internal.Model.BasePagingInfo)">
<summary>
分页
</summary>
<param name="pagingInfo">分页信息</param>
<returns></returns>
</member>
<member name="M:FreeSql.ISelectGrouping`2.Count"> <member name="M:FreeSql.ISelectGrouping`2.Count">
<summary> <summary>
查询的记录数量 查询的记录数量
@ -3976,6 +3990,26 @@
<param name="commandTimeout"></param> <param name="commandTimeout"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="T:FreeSql.Internal.Model.BasePagingInfo">
<summary>
分页信息
</summary>
</member>
<member name="P:FreeSql.Internal.Model.BasePagingInfo.PageNumber">
<summary>
第几页从1开始
</summary>
</member>
<member name="P:FreeSql.Internal.Model.BasePagingInfo.PageSize">
<summary>
每页多少
</summary>
</member>
<member name="P:FreeSql.Internal.Model.BasePagingInfo.Count">
<summary>
查询的记录数量
</summary>
</member>
<member name="P:FreeSql.Internal.Model.BatchProgressStatus`1.Data"> <member name="P:FreeSql.Internal.Model.BatchProgressStatus`1.Data">
<summary> <summary>
当前操作的数据 当前操作的数据

View File

@ -402,6 +402,13 @@ namespace FreeSql
/// <returns></returns> /// <returns></returns>
TSelect Page(int pageNumber, int pageSize); TSelect Page(int pageNumber, int pageSize);
/// <summary>
/// 分页
/// </summary>
/// <param name="pagingInfo">分页信息</param>
/// <returns></returns>
TSelect Page(BasePagingInfo pagingInfo);
/// <summary> /// <summary>
/// 查询数据前,去重 /// 查询数据前,去重
/// <para> /// <para>

View File

@ -1,4 +1,5 @@
using System; using FreeSql.Internal.Model;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using System.Text;
@ -101,6 +102,13 @@ namespace FreeSql
/// <returns></returns> /// <returns></returns>
ISelectGrouping<TKey, TValue> Page(int pageNumber, int pageSize); ISelectGrouping<TKey, TValue> Page(int pageNumber, int pageSize);
/// <summary>
/// 分页
/// </summary>
/// <param name="pagingInfo">分页信息</param>
/// <returns></returns>
ISelectGrouping<TKey, TValue> Page(BasePagingInfo pagingInfo);
/// <summary> /// <summary>
/// 查询的记录数量 /// 查询的记录数量
/// </summary> /// </summary>

View File

@ -179,7 +179,7 @@ namespace FreeSql.Internal.CommonProvider
} }
if (val == null && col.Attribute.MapType == typeof(string) && col.Attribute.IsNullable == false) if (val == null && col.Attribute.MapType == typeof(string) && col.Attribute.IsNullable == false)
col.SetValue(data, val = ""); col.SetValue(data, val = "");
if (val == null && col.Attribute.MapType == typeof(byte[]) && col.Attribute.IsVersion) if (col.Attribute.MapType == typeof(byte[]) && (val == null || (val is byte[] bytes && bytes.Length == 0)) && col.Attribute.IsVersion)
col.SetValue(data, val = Utils.GuidToBytes(Guid.NewGuid())); col.SetValue(data, val = Utils.GuidToBytes(Guid.NewGuid()));
} }
} }

View File

@ -319,6 +319,13 @@ namespace FreeSql.Internal.CommonProvider
return this.Limit(pageSize) as TSelect; 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) public TSelect Skip(int offset)
{ {
_skip = offset; _skip = offset;

View File

@ -201,6 +201,14 @@ namespace FreeSql.Internal.CommonProvider
return this; return this;
} }
public ISelectGrouping<TKey, TValue> 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 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<TKey, TValue> Count(out long count) public ISelectGrouping<TKey, TValue> Count(out long count)
{ {

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace FreeSql.Internal.Model
{
/// <summary>
/// 分页信息
/// </summary>
public class BasePagingInfo
{
/// <summary>
/// 第几页从1开始
/// </summary>
public int PageNumber { get; set; }
/// <summary>
/// 每页多少
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 查询的记录数量
/// </summary>
public long Count { get; set; }
}
}