diff --git a/Jc.Core.Database/Data/Provider/MsSqlDbProvider.cs b/Jc.Core.Database/Data/Provider/MsSqlDbProvider.cs index c8406bf..854cf3a 100644 --- a/Jc.Core.Database/Data/Provider/MsSqlDbProvider.cs +++ b/Jc.Core.Database/Data/Provider/MsSqlDbProvider.cs @@ -76,6 +76,69 @@ public override DbCommand GetQueryRecordsPageDbCommand(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(); + List piMapList = EntityMappingHelper.GetPiMapList(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; + } + + /// + /// 获取分页查询DbCommand + /// + /// + /// 过滤条件 + /// 表名称,如果为空,则使用T对应表名称 + /// + public DbCommand GetQueryRecordsPageDbCommandBak(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" + diff --git a/Jc.Core.Database/Data/Provider/MySqlDbProvider.cs b/Jc.Core.Database/Data/Provider/MySqlDbProvider.cs index c4d0d40..e5c639f 100644 --- a/Jc.Core.Database/Data/Provider/MySqlDbProvider.cs +++ b/Jc.Core.Database/Data/Provider/MySqlDbProvider.cs @@ -83,7 +83,7 @@ public override DbCommand GetQueryRecordsPageDbCommand(QueryFilter filter, st string selectParams = null; string queryStr = null; - string orderStr = null; + string orderStr = ""; EntityMapping dtoDbMapping = EntityMappingHelper.GetMapping(); List piMapList = EntityMappingHelper.GetPiMapList(filter); foreach (FieldMapping piMap in piMapList) @@ -98,10 +98,6 @@ public override DbCommand GetQueryRecordsPageDbCommand(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()); diff --git a/Jc.Core.Database/Data/Provider/PostgreSqlDbProvider.cs b/Jc.Core.Database/Data/Provider/PostgreSqlDbProvider.cs index 185be14..ec8d0ca 100644 --- a/Jc.Core.Database/Data/Provider/PostgreSqlDbProvider.cs +++ b/Jc.Core.Database/Data/Provider/PostgreSqlDbProvider.cs @@ -83,7 +83,7 @@ public override DbCommand GetQueryRecordsPageDbCommand(QueryFilter filter, st string selectParams = null; string queryStr = null; - string orderStr = null; + string orderStr = ""; EntityMapping dtoDbMapping = EntityMappingHelper.GetMapping(); List piMapList = EntityMappingHelper.GetPiMapList(filter); foreach (FieldMapping piMap in piMapList) @@ -98,10 +98,6 @@ public override DbCommand GetQueryRecordsPageDbCommand(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()); diff --git a/Jc.Core.Database/Data/Provider/SqliteDbProvider.cs b/Jc.Core.Database/Data/Provider/SqliteDbProvider.cs index 083ffd7..d7ddf37 100644 --- a/Jc.Core.Database/Data/Provider/SqliteDbProvider.cs +++ b/Jc.Core.Database/Data/Provider/SqliteDbProvider.cs @@ -82,7 +82,7 @@ public override DbCommand GetQueryRecordsPageDbCommand(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(); List piMapList = EntityMappingHelper.GetPiMapList(filter); @@ -98,10 +98,6 @@ public override DbCommand GetQueryRecordsPageDbCommand(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()); diff --git a/Jc.Core.Database/IQuery.cs b/Jc.Core.Database/IQuery.cs index aee6ef4..945510b 100644 --- a/Jc.Core.Database/IQuery.cs +++ b/Jc.Core.Database/IQuery.cs @@ -170,14 +170,7 @@ public IQuery Or(Expression> query = null) public T FirstOrDefault() { Pager pager = new Pager(1,1); - if (orderByClauseList == null || orderByClauseList.Count <= 0) - { - EntityMapping dtoDbMapping = EntityMappingHelper.GetMapping(); - if (dtoDbMapping?.HasPkField == true) - { - orderByClauseList.Add(new OrderByClause(dtoDbMapping.GetPkField().FieldName)); - } - } + QueryFilter filter = QueryFilterBuilder.GetPageFilter(query, select, orderByClauseList,pager, unSelect); T dto = null; @@ -419,8 +412,8 @@ public PageResult ToPageList() try { if (!filter.IsPage) - { - throw new Exception("分页查询未指定分页信息"); + { // 分页查询必须设置分页信息 + throw new Exception("Paging query must specify pagination information"); } else {