瀏覽代碼

Compatible sqlserver2008 (#164)

* Replace FORMAT

* Replace offset

* Use sqlserver 2008

* Remove blank lines

* Tweak UseSqlServer2008
master
keke 6 年之前
committed by Savorboard
父節點
當前提交
7a4a62baf1
共有 2 個檔案被更改,包括 29 行新增5 行删除
  1. +8
    -0
      src/DotNetCore.CAP.SqlServer/CAP.EFOptions.cs
  2. +21
    -5
      src/DotNetCore.CAP.SqlServer/SqlServerMonitoringApi.cs

+ 8
- 0
src/DotNetCore.CAP.SqlServer/CAP.EFOptions.cs 查看文件

@@ -20,5 +20,13 @@ namespace DotNetCore.CAP
/// EF dbcontext type.
/// </summary>
internal Type DbContextType { get; set; }

internal bool IsSqlServer2008 { get; set; }

public EFOptions UseSqlServer2008()
{
IsSqlServer2008 = true;
return this;
}
}
}

+ 21
- 5
src/DotNetCore.CAP.SqlServer/SqlServerMonitoringApi.cs 查看文件

@@ -89,10 +89,17 @@ select count(Id) from [{0}].Received with (nolock) where StatusName = N'Failed';
where += " and content like '%@Content%'";
}

var sqlQuery2008 =
$@"select * from
(SELECT t.*, ROW_NUMBER() OVER(order by t.Added desc) AS rownumber
from [{_options.Schema}].{tableName} as t
where 1=1 {where}) as tbl
where tbl.rownumber between @offset and @offset + @limit";

var sqlQuery =
$"select * from [{_options.Schema}].{tableName} where 1=1 {where} order by Added desc offset @Offset rows fetch next @Limit rows only";

return UseConnection(conn => conn.Query<MessageDto>(sqlQuery, new
return UseConnection(conn => conn.Query<MessageDto>(_options.IsSqlServer2008 ? sqlQuery2008 : sqlQuery, new
{
queryDto.StatusName,
queryDto.Group,
@@ -159,9 +166,18 @@ select count(Id) from [{0}].Received with (nolock) where StatusName = N'Failed';
string statusName,
IDictionary<string, DateTime> keyMaps)
{
//SQL Server 2012+
var sqlQuery =
$@"
var sqlQuery2008 = $@"
with aggr as (
select replace(convert(varchar, Added, 111), '/','-') + '-' + CONVERT(varchar, DATEPART(hh, Added)) as [Key],
count(id) [Count]
from [{_options.Schema}].{tableName}
where StatusName = @statusName
group by replace(convert(varchar, Added, 111), '/','-') + '-' + CONVERT(varchar, DATEPART(hh, Added))
)
select [Key], [Count] from aggr with (nolock) where [Key] in @keys;";

//SQL Server 2012+
var sqlQuery = $@"
with aggr as (
select FORMAT(Added,'yyyy-MM-dd-HH') as [Key],
count(id) [Count]
@@ -172,7 +188,7 @@ with aggr as (
select [Key], [Count] from aggr with (nolock) where [Key] in @keys;";

var valuesMap = connection.Query<TimelineCounter>(
sqlQuery,
_options.IsSqlServer2008 ? sqlQuery2008 : sqlQuery,
new { keys = keyMaps.Keys, statusName })
.ToDictionary(x => x.Key, x => x.Count);



Loading…
取消
儲存