-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathIdentityServerDapperExtensions.cs
More file actions
63 lines (59 loc) · 2.36 KB
/
Copy pathIdentityServerDapperExtensions.cs
File metadata and controls
63 lines (59 loc) · 2.36 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
63
using IdentityServer4.Dapper.Options;
using Microsoft.Extensions.DependencyInjection;
using System;
using Npgsql;
using System.Linq;
namespace IdentityServer4.Dapper.Extensions.PostgreSQL
{
public static class IdentityServerDapperDBExtensions
{
public static IIdentityServerBuilder AddPostgreSQLProvider(this IIdentityServerBuilder builder, Action<DBProviderOptions> dbProviderOptionsAction = null)
{
var options = GetDefaultOptions();
dbProviderOptionsAction?.Invoke(options);
builder.Services.AddSingleton(options);
return builder;
}
public static DBProviderOptions GetDefaultOptions()
{
//config mysql
var options = new DBProviderOptions();
options.DbProviderFactory = NpgsqlFactory.Instance;
//get last insert id for insert actions
options.GetLastInsertID = "select LASTVAL();";
//config the ColumnName protect string, postgresql using ""
options.ColumnProtect = new System.Collections.Generic.Dictionary<string, string>();
options.ColumnProtect.Add("left", "");
options.ColumnProtect.Add("right", "");
options.GetInArray = " = ANY ";
//add singgleton
options.GetPageQuerySQL = (input, pageindex, pagesize, totalcount, orderby, pairs) =>
{
string limitsql = string.Empty;
if (pagesize > 0)
{
if (pagesize > totalcount)
{
pagesize = totalcount;
}
pairs.Add("start", (pageindex - 1) * pagesize);
pairs.Add("size", pagesize);
limitsql = "limit @size offset @start";
}
if (input.IndexOf("order by", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
orderby = "";
}
else
{
if (!string.IsNullOrWhiteSpace(orderby) && orderby.IndexOf("order by", StringComparison.CurrentCultureIgnoreCase) < 0)
{
orderby = "order by " + orderby;
}
}
return $"{input} {orderby} {limitsql}";
};
return options;
}
}
}