Skip to content

Commit

Permalink
1.取消分页查询必须指定排序或指定主键限制
Browse files Browse the repository at this point in the history
2.修改SqlServer分页查询方式
  • Loading branch information
279328316 committed Sep 6, 2023
1 parent 8bf209d commit 96fd424
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 25 deletions.
63 changes: 63 additions & 0 deletions Jc.Core.Database/Data/Provider/MsSqlDbProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,69 @@ public override DbCommand GetQueryRecordsPageDbCommand<T>(QueryFilter filter, st
{
DbCommand dbCommand = CreateDbCommand();

//表名 查询字段名 主键字段名
string sqlStr = "Select {1} From {0} t1 @OrderStr offset @LowRecNum rows fetch next @PageSize rows only";
string selectParams = null;

string queryStr = null;
string orderStr = null;

EntityMapping dtoDbMapping = EntityMappingHelper.GetMapping<T>();
List<FieldMapping> piMapList = EntityMappingHelper.GetPiMapList<T>(filter);
foreach (FieldMapping piMap in piMapList)
{
selectParams += string.IsNullOrEmpty(selectParams) ? "t2.Num,t1." + piMap.FieldName : ",t1." + piMap.FieldName;
}
if (filter != null && filter.ItemList.Count>0)
{
queryStr = filter.FilterSQLString;
}
if (filter != null && !string.IsNullOrEmpty(filter.OrderSQLString))
{
orderStr = filter.OrderSQLString;
}
else
{ //默认By 主键 Asc
if (dtoDbMapping.HasPkField)
{
orderStr = $"order by {dtoDbMapping.GetPkField().FieldName} asc";
}
else
{
throw new Exception("Paging queries for sqlserver must specify sorting fields");
}
}
sqlStr = sqlStr.Replace("@QueryStr", queryStr);
sqlStr = sqlStr.Replace("@LowRecNum", filter.FilterStartIndex.ToString());
sqlStr = sqlStr.Replace("@PageSize", filter.PageSize.ToString());
sqlStr = sqlStr.Replace("@OrderStr", orderStr);
if (filter != null)
{
for (int i = 0; i < filter.FilterParameters.Count; i++)
{
DbParameter dbParameter = dbCommand.CreateParameter();
dbParameter.Direction = ParameterDirection.Input;
dbParameter.ParameterName = filter.FilterParameters[i].ParameterName;
dbParameter.Value = filter.FilterParameters[i].ParameterValue;
dbParameter.DbType = filter.FilterParameters[i].ParameterDbType;
dbCommand.Parameters.Add(dbParameter);
}
}
dbCommand.CommandText = string.Format(sqlStr, dtoDbMapping.GetTableName(subTableArg), selectParams);
return dbCommand;
}

/// <summary>
/// 获取分页查询DbCommand
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filter">过滤条件</param>
/// <param name="subTableArg">表名称,如果为空,则使用T对应表名称</param>
/// <returns></returns>
public DbCommand GetQueryRecordsPageDbCommandBak<T>(QueryFilter filter, string subTableArg = null)
{
DbCommand dbCommand = CreateDbCommand();

//表名 查询字段名 主键字段名
string sqlStr = "Select {1} From {0} t1" +
",(Select Top (@UpRecNum) row_number() OVER (@OrderStr) Num,{2} From {0} @QueryStr) t2" +
Expand Down
6 changes: 1 addition & 5 deletions Jc.Core.Database/Data/Provider/MySqlDbProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public override DbCommand GetQueryRecordsPageDbCommand<T>(QueryFilter filter, st
string selectParams = null;

string queryStr = null;
string orderStr = null;
string orderStr = "";
EntityMapping dtoDbMapping = EntityMappingHelper.GetMapping<T>();
List<FieldMapping> piMapList = EntityMappingHelper.GetPiMapList<T>(filter);
foreach (FieldMapping piMap in piMapList)
Expand All @@ -98,10 +98,6 @@ public override DbCommand GetQueryRecordsPageDbCommand<T>(QueryFilter filter, st
{
orderStr = filter.OrderSQLString;
}
else
{ //默认By 主键 Asc
orderStr = "order by {2} asc";
}
sqlStr = sqlStr.Replace("@QueryStr", queryStr);
sqlStr = sqlStr.Replace("@LowRecNum", filter.FilterStartIndex.ToString());
sqlStr = sqlStr.Replace("@PageSize", filter.PageSize.ToString());
Expand Down
6 changes: 1 addition & 5 deletions Jc.Core.Database/Data/Provider/PostgreSqlDbProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public override DbCommand GetQueryRecordsPageDbCommand<T>(QueryFilter filter, st
string selectParams = null;

string queryStr = null;
string orderStr = null;
string orderStr = "";
EntityMapping dtoDbMapping = EntityMappingHelper.GetMapping<T>();
List<FieldMapping> piMapList = EntityMappingHelper.GetPiMapList<T>(filter);
foreach (FieldMapping piMap in piMapList)
Expand All @@ -98,10 +98,6 @@ public override DbCommand GetQueryRecordsPageDbCommand<T>(QueryFilter filter, st
{
orderStr = filter.OrderSQLString;
}
else
{ //默认By 主键 Asc
orderStr = "order by {2} asc";
}
sqlStr = sqlStr.Replace("@QueryStr", queryStr);
sqlStr = sqlStr.Replace("@LowRecNum", filter.FilterStartIndex.ToString());
sqlStr = sqlStr.Replace("@PageSize", filter.PageSize.ToString());
Expand Down
6 changes: 1 addition & 5 deletions Jc.Core.Database/Data/Provider/SqliteDbProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override DbCommand GetQueryRecordsPageDbCommand<T>(QueryFilter filter, st
string sqlStr = "Select {1} From {0} @QueryStr @OrderStr limit @PageSize offset @LowRecNum";
string selectParams = null;
string queryStr = null;
string orderStr = null;
string orderStr = "";

EntityMapping dtoDbMapping = EntityMappingHelper.GetMapping<T>();
List<FieldMapping> piMapList = EntityMappingHelper.GetPiMapList<T>(filter);
Expand All @@ -98,10 +98,6 @@ public override DbCommand GetQueryRecordsPageDbCommand<T>(QueryFilter filter, st
{
orderStr = filter.OrderSQLString;
}
else
{ //默认By 主键 Asc
orderStr = "order by {2} asc";
}
sqlStr = sqlStr.Replace("@QueryStr", queryStr);
sqlStr = sqlStr.Replace("@LowRecNum", filter.FilterStartIndex.ToString());
sqlStr = sqlStr.Replace("@PageSize", filter.PageSize.ToString());
Expand Down
13 changes: 3 additions & 10 deletions Jc.Core.Database/IQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,7 @@ public IQuery<T> Or(Expression<Func<T, bool>> query = null)
public T FirstOrDefault()
{
Pager pager = new Pager(1,1);
if (orderByClauseList == null || orderByClauseList.Count <= 0)
{
EntityMapping dtoDbMapping = EntityMappingHelper.GetMapping<T>();
if (dtoDbMapping?.HasPkField == true)
{
orderByClauseList.Add(new OrderByClause(dtoDbMapping.GetPkField().FieldName));
}
}

QueryFilter filter = QueryFilterBuilder.GetPageFilter(query, select, orderByClauseList,pager, unSelect);

T dto = null;
Expand Down Expand Up @@ -419,8 +412,8 @@ public PageResult<T> ToPageList()
try
{
if (!filter.IsPage)
{
throw new Exception("分页查询未指定分页信息");
{ // 分页查询必须设置分页信息
throw new Exception("Paging query must specify pagination information");
}
else
{
Expand Down

0 comments on commit 96fd424

Please sign in to comment.