Skip to content

Commit

Permalink
PagedQueryOptions增加AlwayQueryCount属性
Browse files Browse the repository at this point in the history
  • Loading branch information
junjie-sun committed Oct 22, 2019
1 parent 2d3ee93 commit e491e7e
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 3 deletions.
6 changes: 5 additions & 1 deletion doc/更新日志.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,8 @@ XDbAccess.MySql
XDbAccess Tag��1.3.2

XDbAccess.Common
MSSqlSQLBuilder��MySqlSQLBuilder���BuidlPagedQuerySql��BuildQueryCountSql�����Ż�Where�Ӿ�������߼�
MSSqlSQLBuilder��MySqlSQLBuilder���BuidlPagedQuerySql��BuildQueryCountSql�����Ż�Where�Ӿ�������߼�
PagedQueryOptions������AlwayQueryCount����

XDbAccess.Dapper
DbHelper���PagedQuery��������������AlwayQueryCount���Խ�����Ӧ�޸�
5 changes: 5 additions & 0 deletions src/XDbAccess.Common/Page/PagedQueryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@ public class PagedQueryOptions
/// 示例:[Field1],[Field2] desc
/// </summary>
public string SqlOrderPart { get; set; }

/// <summary>
/// 是否每次都查询记录总数
/// </summary>
public bool AlwayQueryCount { get; set; } = false;
}
}
5 changes: 5 additions & 0 deletions src/XDbAccess.Common/XDbAccess.Common.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/XDbAccess.Dapper/DbHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ public DbHelper(DbContextImpl dbContext)
result.PageIndex = options.PageIndex;
result.PageSize = options.PageSize;

if (options.PageIndex == 0)
if (options.PageIndex == 0 || options.AlwayQueryCount)
{
var countSql = SQLBuilder.BuildQueryCountSql(options.SqlFromPart, options.SqlConditionPart);
result.Total = this.ExecuteScalar<long>(countSql, param, commandTimeout);
Expand All @@ -728,7 +728,7 @@ public DbHelper(DbContextImpl dbContext)
result.PageIndex = options.PageIndex;
result.PageSize = options.PageSize;

if (options.PageIndex == 0)
if (options.PageIndex == 0 || options.AlwayQueryCount)
{
var countSql = SQLBuilder.BuildQueryCountSql(options.SqlFromPart, options.SqlConditionPart);
result.Total = await this.ExecuteScalarAsync<long>(countSql, param, commandTimeout);
Expand Down
62 changes: 62 additions & 0 deletions src/XDbAccess.Test/UnitTests/MSSqlDbHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,37 @@ insert into [user](Name,Birthday,Description,OrgId)
var user = result.Data[1];
Assert.Equal("PagedQueryUser5", user.Name);
Assert.Equal("PagedQueryOrg", user.OrgName);

//测试第2页
result = DbHelper.PagedQuery<OrgUser>(new PagedQueryOptions()
{
PageIndex = 1,
PageSize = 3,
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(0, result.Total);
user = result.Data[1];
Assert.Equal("PagedQueryUser8", user.Name);
Assert.Equal("PagedQueryOrg", user.OrgName);

//测试第2页及AlwayQueryCount
result = DbHelper.PagedQuery<OrgUser>(new PagedQueryOptions()
{
PageIndex = 1,
PageSize = 3,
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlOrderPart = "u.Id",
AlwayQueryCount = true
}, new { UserId = 3 });
Assert.Equal(17, result.Total);
user = result.Data[1];
Assert.Equal("PagedQueryUser8", user.Name);
Assert.Equal("PagedQueryOrg", user.OrgName);
}

[Fact]
Expand Down Expand Up @@ -1163,6 +1194,37 @@ insert into [user](Name,Birthday,Description,OrgId)
var user = result.Data[1];
Assert.Equal("PagedQueryAsyncUser5", user.Name);
Assert.Equal("PagedQueryAsyncOrg", user.OrgName);

//测试第2页
result = await DbHelper.PagedQueryAsync<OrgUser>(new PagedQueryOptions()
{
PageIndex = 1,
PageSize = 3,
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(0, result.Total);
user = result.Data[1];
Assert.Equal("PagedQueryAsyncUser8", user.Name);
Assert.Equal("PagedQueryAsyncOrg", user.OrgName);

//测试第2页及AlwayQueryCount
result = await DbHelper.PagedQueryAsync<OrgUser>(new PagedQueryOptions()
{
PageIndex = 1,
PageSize = 3,
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlOrderPart = "u.Id",
AlwayQueryCount = true
}, new { UserId = 3 });
Assert.Equal(17, result.Total);
user = result.Data[1];
Assert.Equal("PagedQueryAsyncUser8", user.Name);
Assert.Equal("PagedQueryAsyncOrg", user.OrgName);
}

#endregion
Expand Down
62 changes: 62 additions & 0 deletions src/XDbAccess.Test/UnitTests/MySqlDbHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,37 @@ public void PagedQueryTest()
var user = result.Data[1];
Assert.Equal("PagedQueryUser5", user.Name);
Assert.Equal("PagedQueryOrg", user.OrgName);

//测试第2页
result = DbHelper.PagedQuery<OrgUser>(new PagedQueryOptions()
{
PageIndex = 1,
PageSize = 3,
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(0, result.Total);
user = result.Data[1];
Assert.Equal("PagedQueryUser8", user.Name);
Assert.Equal("PagedQueryOrg", user.OrgName);

//测试第2页及AlwayQueryCount
result = DbHelper.PagedQuery<OrgUser>(new PagedQueryOptions()
{
PageIndex = 1,
PageSize = 3,
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlOrderPart = "u.Id",
AlwayQueryCount = true
}, new { UserId = 3 });
Assert.Equal(17, result.Total);
user = result.Data[1];
Assert.Equal("PagedQueryUser8", user.Name);
Assert.Equal("PagedQueryOrg", user.OrgName);
}

[Fact]
Expand Down Expand Up @@ -1259,6 +1290,37 @@ public async void PagedQueryAsyncTest()
var user = result.Data[1];
Assert.Equal("PagedQueryAsyncUser5", user.Name);
Assert.Equal("PagedQueryAsyncOrg", user.OrgName);

//测试第2页
result = await DbHelper.PagedQueryAsync<OrgUser>(new PagedQueryOptions()
{
PageIndex = 1,
PageSize = 3,
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(0, result.Total);
user = result.Data[1];
Assert.Equal("PagedQueryAsyncUser8", user.Name);
Assert.Equal("PagedQueryAsyncOrg", user.OrgName);

//测试第2页及AlwayQueryCount
result = await DbHelper.PagedQueryAsync<OrgUser>(new PagedQueryOptions()
{
PageIndex = 1,
PageSize = 3,
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlOrderPart = "u.Id",
AlwayQueryCount = true
}, new { UserId = 3 });
Assert.Equal(17, result.Total);
user = result.Data[1];
Assert.Equal("PagedQueryAsyncUser8", user.Name);
Assert.Equal("PagedQueryAsyncOrg", user.OrgName);
}

#endregion
Expand Down

0 comments on commit e491e7e

Please sign in to comment.