From e491e7eca4f647a7adeb3d4144a7011a6efcacd7 Mon Sep 17 00:00:00 2001 From: Junjie-Sun Date: Wed, 23 Oct 2019 01:36:18 +0800 Subject: [PATCH] =?UTF-8?q?PagedQueryOptions=E5=A2=9E=E5=8A=A0AlwayQueryCo?= =?UTF-8?q?unt=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\346\226\260\346\227\245\345\277\227.txt" | 6 +- .../Page/PagedQueryOptions.cs | 5 ++ src/XDbAccess.Common/XDbAccess.Common.xml | 5 ++ src/XDbAccess.Dapper/DbHelper.cs | 4 +- .../UnitTests/MSSqlDbHelperTest.cs | 62 +++++++++++++++++++ .../UnitTests/MySqlDbHelperTest.cs | 62 +++++++++++++++++++ 6 files changed, 141 insertions(+), 3 deletions(-) diff --git "a/doc/\346\233\264\346\226\260\346\227\245\345\277\227.txt" "b/doc/\346\233\264\346\226\260\346\227\245\345\277\227.txt" index aa7d6fa..836968f 100644 --- "a/doc/\346\233\264\346\226\260\346\227\245\345\277\227.txt" +++ "b/doc/\346\233\264\346\226\260\346\227\245\345\277\227.txt" @@ -129,4 +129,8 @@ XDbAccess.MySql XDbAccess Tag:1.3.2 XDbAccess.Common -MSSqlSQLBuilder和MySqlSQLBuilder类的BuidlPagedQuerySql与BuildQueryCountSql方法优化Where子句的生成逻辑 \ No newline at end of file +MSSqlSQLBuilder和MySqlSQLBuilder类的BuidlPagedQuerySql与BuildQueryCountSql方法优化Where子句的生成逻辑 +PagedQueryOptions类增加AlwayQueryCount属性 + +XDbAccess.Dapper +DbHelper类的PagedQuery方法根据新增的AlwayQueryCount属性进行相应修改 \ No newline at end of file diff --git a/src/XDbAccess.Common/Page/PagedQueryOptions.cs b/src/XDbAccess.Common/Page/PagedQueryOptions.cs index 1647bc2..d3115c1 100644 --- a/src/XDbAccess.Common/Page/PagedQueryOptions.cs +++ b/src/XDbAccess.Common/Page/PagedQueryOptions.cs @@ -46,5 +46,10 @@ public class PagedQueryOptions /// 绀轰緥锛歔Field1],[Field2] desc /// public string SqlOrderPart { get; set; } + + /// + /// 鏄惁姣忔閮芥煡璇㈣褰曟绘暟 + /// + public bool AlwayQueryCount { get; set; } = false; } } diff --git a/src/XDbAccess.Common/XDbAccess.Common.xml b/src/XDbAccess.Common/XDbAccess.Common.xml index 41456e6..e92d754 100644 --- a/src/XDbAccess.Common/XDbAccess.Common.xml +++ b/src/XDbAccess.Common/XDbAccess.Common.xml @@ -157,6 +157,11 @@ 绀轰緥锛歔Field1],[Field2] desc + + + 鏄惁姣忔閮芥煡璇㈣褰曟绘暟 + + 鍒嗛〉鏌ヨ缁撴灉 diff --git a/src/XDbAccess.Dapper/DbHelper.cs b/src/XDbAccess.Dapper/DbHelper.cs index 10f19df..a993428 100644 --- a/src/XDbAccess.Dapper/DbHelper.cs +++ b/src/XDbAccess.Dapper/DbHelper.cs @@ -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(countSql, param, commandTimeout); @@ -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(countSql, param, commandTimeout); diff --git a/src/XDbAccess.Test/UnitTests/MSSqlDbHelperTest.cs b/src/XDbAccess.Test/UnitTests/MSSqlDbHelperTest.cs index f28dfae..695a215 100644 --- a/src/XDbAccess.Test/UnitTests/MSSqlDbHelperTest.cs +++ b/src/XDbAccess.Test/UnitTests/MSSqlDbHelperTest.cs @@ -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(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(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] @@ -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(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(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 diff --git a/src/XDbAccess.Test/UnitTests/MySqlDbHelperTest.cs b/src/XDbAccess.Test/UnitTests/MySqlDbHelperTest.cs index c8dbece..84399db 100644 --- a/src/XDbAccess.Test/UnitTests/MySqlDbHelperTest.cs +++ b/src/XDbAccess.Test/UnitTests/MySqlDbHelperTest.cs @@ -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(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(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] @@ -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(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(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