Skip to content

Commit

Permalink
BuildQueryCountSql增加GroupPart参数
Browse files Browse the repository at this point in the history
  • Loading branch information
junjie-sun committed Nov 14, 2019
1 parent 89bcccf commit 2d26174
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/XDbAccess.Common/SQLBuilder/ISQLBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public interface ISQLBuilder
/// </summary>
/// <param name="sqlFromPart">FROM部分的SQL</param>
/// <param name="sqlConditionPart">WHERE部分的SQL</param>
/// <param name="sqlGroupPart">GROUP部分的SQL</param>
/// <returns></returns>
string BuildQueryCountSql(string sqlFromPart, string sqlConditionPart = null);
string BuildQueryCountSql(string sqlFromPart, string sqlConditionPart = null, string sqlGroupPart = null);

/// <summary>
/// 构造SELECT语句
Expand Down
16 changes: 12 additions & 4 deletions src/XDbAccess.Common/SQLBuilder/MSSqlSQLBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public string BuidlPagedQuerySql(PagedQueryOptions options)
var sql = string.Format(@"SELECT * FROM (
SELECT {0},ROW_NUMBER() OVER(ORDER BY {1}) AS RowNumber FROM {2}
{3} {4}
) as pageTable where RowNumber>={5} and RowNumber<={6};",
) as PageTable where RowNumber>={5} and RowNumber<={6};",
options.SqlFieldsPart, options.SqlOrderPart, options.SqlFromPart,
string.IsNullOrEmpty(options.SqlConditionPart) ? string.Empty : "WHERE " + options.SqlConditionPart,
string.IsNullOrEmpty(options.SqlGroupPart) ? string.Empty : "GROUP BY " + options.SqlGroupPart,
Expand All @@ -206,16 +206,24 @@ public string BuidlPagedQuerySql(PagedQueryOptions options)
/// </summary>
/// <param name="sqlFromPart">FROM部分的SQL</param>
/// <param name="sqlConditionPart">>WHERE部分的SQL</param>
/// <param name="sqlGroupPart">GROUP部分的SQL</param>
/// <returns></returns>
public string BuildQueryCountSql(string sqlFromPart, string sqlConditionPart = null)
public string BuildQueryCountSql(string sqlFromPart, string sqlConditionPart = null, string sqlGroupPart = null)
{
if (string.IsNullOrEmpty(sqlFromPart))
{
throw new ArgumentNullException("Need to specify sqlFromPart");
}

var sql = string.Format(" SELECT COUNT(1) FROM {0} where {1};"
, sqlFromPart, sqlConditionPart);
var sql = string.Format(@"
SELECT COUNT(1) FROM
(
SELECT 1 as CountField
FROM {0} {1} {2}
) as CountTable;
", sqlFromPart,
string.IsNullOrEmpty(sqlConditionPart) ? string.Empty : "WHERE " + sqlConditionPart,
string.IsNullOrEmpty(sqlGroupPart) ? string.Empty : "GROUP BY " + sqlGroupPart);

return sql;
}
Expand Down
14 changes: 11 additions & 3 deletions src/XDbAccess.Common/SQLBuilder/MySqlSQLBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,24 @@ public string BuidlPagedQuerySql(PagedQueryOptions options)
/// </summary>
/// <param name="sqlFromPart">FROM部分的SQL</param>
/// <param name="sqlConditionPart">WHERE部分的SQL</param>
/// <param name="sqlGroupPart">GROUP部分的SQL</param>
/// <returns></returns>
public string BuildQueryCountSql(string sqlFromPart, string sqlConditionPart = null)
public string BuildQueryCountSql(string sqlFromPart, string sqlConditionPart = null, string sqlGroupPart = null)
{
if (string.IsNullOrEmpty(sqlFromPart))
{
throw new ArgumentNullException("Need to specify sqlFromPart");
}

var sql = string.Format(" SELECT COUNT(1) FROM {0} where {1};"
, sqlFromPart, sqlConditionPart);
var sql = string.Format(@"
SELECT COUNT(1) FROM
(
SELECT 1 as CountField
FROM {0} {1} {2}
) as CountTable;
", sqlFromPart,
string.IsNullOrEmpty(sqlConditionPart) ? string.Empty : "WHERE " + sqlConditionPart,
string.IsNullOrEmpty(sqlGroupPart) ? string.Empty : "GROUP BY " + sqlGroupPart);

return sql;
}
Expand Down
9 changes: 6 additions & 3 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 @@ -793,7 +793,7 @@ public DbHelper(DbContextImpl dbContext)

if (options.PageIndex == 0 || options.AlwayQueryCount)
{
var countSql = SQLBuilder.BuildQueryCountSql(options.SqlFromPart, options.SqlConditionPart);
var countSql = SQLBuilder.BuildQueryCountSql(options.SqlFromPart, options.SqlConditionPart, options.SqlGroupPart);
result.Total = this.ExecuteScalar<long>(countSql, param, commandTimeout);
}

Expand All @@ -820,7 +820,7 @@ public DbHelper(DbContextImpl dbContext)

if (options.PageIndex == 0 || options.AlwayQueryCount)
{
var countSql = SQLBuilder.BuildQueryCountSql(options.SqlFromPart, options.SqlConditionPart);
var countSql = SQLBuilder.BuildQueryCountSql(options.SqlFromPart, options.SqlConditionPart, options.SqlGroupPart);
result.Total = await this.ExecuteScalarAsync<long>(countSql, param, commandTimeout);
}

Expand Down
32 changes: 26 additions & 6 deletions src/XDbAccess.Test/UnitTests/MSSqlDbHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,6 @@ insert into [user](Name,Birthday,Description,OrgId)
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(17, result.Total);
Expand All @@ -1383,7 +1382,6 @@ insert into [user](Name,Birthday,Description,OrgId)
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(0, result.Total);
Expand All @@ -1399,14 +1397,26 @@ insert into [user](Name,Birthday,Description,OrgId)
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name",
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);

//测试Group及ConditionPart=null
result = DbHelper.PagedQuery<OrgUser>(new PagedQueryOptions()
{
PageIndex = 0,
PageSize = 3,
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.OrgId",
SqlGroupPart = "u.OrgId",
SqlOrderPart = "u.OrgId"
}, new { UserId = 3 });
Assert.Equal(1, result.Total);
Assert.Single(result.Data);
}

[Fact]
Expand Down Expand Up @@ -1441,7 +1451,6 @@ insert into [user](Name,Birthday,Description,OrgId)
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(17, result.Total);
Expand All @@ -1457,7 +1466,6 @@ insert into [user](Name,Birthday,Description,OrgId)
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(0, result.Total);
Expand All @@ -1473,14 +1481,26 @@ insert into [user](Name,Birthday,Description,OrgId)
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.OrgId,o.Name",
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);

//测试Group及ConditionPart=null
result = await DbHelper.PagedQueryAsync<OrgUser>(new PagedQueryOptions()
{
PageIndex = 0,
PageSize = 3,
SqlFromPart = "[org] o join [user] u on o.Id=u.OrgId",
SqlFieldsPart = "u.OrgId",
SqlGroupPart = "u.OrgId",
SqlOrderPart = "u.OrgId"
}, new { UserId = 3 });
Assert.Equal(1, result.Total);
Assert.Single(result.Data);
}

#endregion
Expand Down
32 changes: 26 additions & 6 deletions src/XDbAccess.Test/UnitTests/MySqlDbHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,6 @@ public void PagedQueryTest()
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.Description,u.OrgId,o.Name",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(17, result.Total);
Expand All @@ -1480,7 +1479,6 @@ public void PagedQueryTest()
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.Description,u.OrgId,o.Name",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(0, result.Total);
Expand All @@ -1496,14 +1494,26 @@ public void PagedQueryTest()
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.Description,u.OrgId,o.Name",
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);

//测试Group及ConditionPart=null
result = DbHelper.PagedQuery<OrgUser>(new PagedQueryOptions()
{
PageIndex = 0,
PageSize = 3,
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.OrgId",
SqlGroupPart = "u.OrgId",
SqlOrderPart = "u.OrgId"
}, new { UserId = 3 });
Assert.Equal(1, result.Total);
Assert.Single(result.Data);
}

[Fact]
Expand Down Expand Up @@ -1538,7 +1548,6 @@ public async void PagedQueryAsyncTest()
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.Description,u.OrgId,o.Name",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(17, result.Total);
Expand All @@ -1554,7 +1563,6 @@ public async void PagedQueryAsyncTest()
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.Description,u.OrgId,o.Name",
SqlOrderPart = "u.Id"
}, new { UserId = 3 });
Assert.Equal(0, result.Total);
Expand All @@ -1570,14 +1578,26 @@ public async void PagedQueryAsyncTest()
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.*,o.Name OrgName",
SqlConditionPart = "u.Id > @UserId",
SqlGroupPart = "u.Id,u.Name,u.Birthday,u.Description,u.OrgId,o.Name",
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);

//测试Group及ConditionPart=null
result = await DbHelper.PagedQueryAsync<OrgUser>(new PagedQueryOptions()
{
PageIndex = 0,
PageSize = 3,
SqlFromPart = "`org` o join `user` u on o.Id=u.OrgId",
SqlFieldsPart = "u.OrgId",
SqlGroupPart = "u.OrgId",
SqlOrderPart = "u.OrgId"
}, new { UserId = 3 });
Assert.Equal(1, result.Total);
Assert.Single(result.Data);
}

#endregion
Expand Down

0 comments on commit 2d26174

Please sign in to comment.