-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathIdentityServerDapperExtensions.cs
More file actions
62 lines (56 loc) · 2.46 KB
/
Copy pathIdentityServerDapperExtensions.cs
File metadata and controls
62 lines (56 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using IdentityServer4.Dapper.Options;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Data.SqlClient;
namespace IdentityServer4.Dapper.Extensions.MSSql
{
public static class IdentityServerDapperDBExtensions
{
public static IIdentityServerBuilder AddMSSQLProvider(this IIdentityServerBuilder builder, Action<DBProviderOptions> dbProviderOptionsAction = null)
{
var options = GetDefaultOptions();
dbProviderOptionsAction?.Invoke(options);
builder.Services.AddSingleton(options);
return builder;
}
public static DBProviderOptions GetDefaultOptions()
{
//config mssql
var options = new DBProviderOptions();
options.DbProviderFactory = SqlClientFactory.Instance;
//get last insert id for insert actions
options.GetLastInsertID = "select @@IDENTITY;";
//config the ColumnName protect string, mssql using "[]"
options.ColumnProtect = new System.Collections.Generic.Dictionary<string, string>();
options.ColumnProtect.Add("left", "[");
options.ColumnProtect.Add("right", "]");
options.GetInArray = " in ";
options.GetPageQuerySQL = (input, pageindex, pagesize, totalcount, orderby, pairs) =>
{
int pagestart = 0;
int pageend = 0;
string limitsql = string.Empty;
if (pagesize > 0)
{
if (pagesize > totalcount)
{
pagesize = totalcount;
}
pagestart = (pageindex - 1) * pagesize + 1;
pageend = pagestart - 1 + pagesize;
}
if (string.IsNullOrWhiteSpace(orderby))
{
orderby = "order by id"; //default
}
if (!string.IsNullOrWhiteSpace(orderby) && orderby.IndexOf("order by", StringComparison.CurrentCultureIgnoreCase) < 0)
{
orderby = "order by " + orderby;
}
input = $"select ROW_NUMBER() over ({orderby}) as rowid,{input.Substring(input.IndexOf("select", StringComparison.CurrentCultureIgnoreCase) + 6)}";
return $"select * from ({input}) as innertable where rowid between {pagestart} and {pageend};";
};
return options;
}
}
}