Skip to content

Commit c44f6b4

Browse files
committed
Add SQL table creator
1 parent b32f0ed commit c44f6b4

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

src/DynamicAuthorization.Mvc.JsonStore/Extensions/DynamicAuthorizationJsonBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace DynamicAuthorization.Mvc.JsonStore.Extensions
99
{
10-
public static class ServiceCollectionExtensions
10+
public static class DynamicAuthorizationBuilderExtensions
1111
{
1212
private static readonly string Directory = Path.GetDirectoryName(typeof(RoleAccessStore).GetTypeInfo().Assembly.Location);
1313

src/DynamicAuthorization.Mvc.MsSqlServerStore/DynamicAuthorization.Mvc.MsSqlServerStore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<ItemGroup>
88
<PackageReference Include="Dapper" Version="2.0.30" />
9+
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.1" />
910
</ItemGroup>
1011

1112
<ItemGroup>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using DynamicAuthorization.Mvc.Core;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
6+
namespace DynamicAuthorization.Mvc.MsSqlServerStore
7+
{
8+
public static class DynamicAuthorizationBuilderExtensions
9+
{
10+
public static IDynamicAuthorizationBuilder AddSqlServerStore(this IDynamicAuthorizationBuilder builder, Action<SqlOptions> options)
11+
{
12+
if (builder == null)
13+
throw new ArgumentNullException(nameof(builder));
14+
15+
var sqlOptions = new SqlOptions();
16+
options.Invoke(sqlOptions);
17+
18+
var serviceProvider = builder.Services.BuildServiceProvider();
19+
var scope = serviceProvider.CreateScope();
20+
var logger = scope.ServiceProvider.GetService<ILogger<SqlTableCreator>>();
21+
22+
var tableCreator = new SqlTableCreator(sqlOptions, logger);
23+
tableCreator.CreateTable();
24+
25+
scope.Dispose();
26+
serviceProvider.Dispose();
27+
28+
builder.Services.AddSingleton(sqlOptions);
29+
builder.Services.AddScoped<IRoleAccessStore, RoleAccessStore>();
30+
31+
return builder;
32+
}
33+
}
34+
}

src/DynamicAuthorization.Mvc.MsSqlServerStore/SqlTableCreator.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
using DynamicAuthorization.Mvc.Core.Models;
2+
using Microsoft.Data.SqlClient;
3+
using Microsoft.Extensions.Logging;
24
using System;
35
using System.Text;
46

57
namespace DynamicAuthorization.Mvc.MsSqlServerStore
68
{
79
internal class SqlTableCreator
810
{
11+
private readonly SqlOptions _options;
12+
private readonly ILogger<SqlTableCreator> _logger;
13+
14+
public SqlTableCreator(SqlOptions options, ILogger<SqlTableCreator> logger)
15+
{
16+
_options = options;
17+
_logger = logger;
18+
}
19+
920
public void CreateTable()
1021
{
1122
try
1223
{
13-
using (var conn = _sqlConnectionFactory.Create())
24+
using (var conn = new SqlConnection(_options.ConnectionString))
1425
{
1526
var sql = GetTableDdl();
16-
using (var cmd = conn.CreateCommand(sql))
27+
using (var cmd = new SqlCommand(sql, conn))
1728
{
1829
conn.Open();
1930
cmd.ExecuteNonQuery();
@@ -22,7 +33,7 @@ public void CreateTable()
2233
}
2334
catch (Exception ex)
2435
{
25-
Console.WriteLine($"Exception creating table AccessRole:\n{ex}");
36+
_logger.LogError(ex, "An error has occurred while creating RoleAccess table");
2637
}
2738
}
2839

@@ -37,8 +48,8 @@ private string GetTableDdl()
3748
sql.AppendLine("[Access] nvarchar(max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL );");
3849
sql.AppendLine("ALTER TABLE [dbo].[RoleAccess] SET (LOCK_ESCALATION = TABLE);");
3950
sql.AppendLine("DBCC CHECKIDENT ('[dbo].[RoleAccess]', RESEED, 1);");
40-
sql.AppendLine("CREATE NONCLUSTERED INDEX [IX_AspNetRoleClaims_RoleId] ");
41-
sql.AppendLine("ON [dbo].[AspNetRoleClaims] ([RoleId] ASC);");
51+
sql.AppendLine("CREATE NONCLUSTERED INDEX [IX_RoleAccess_RoleId] ");
52+
sql.AppendLine("ON [dbo].[RoleAccess] ([RoleId] ASC);");
4253
sql.AppendLine("ALTER TABLE [dbo].[RoleAccess] ADD CONSTRAINT [PK_RoleAccess] PRIMARY KEY CLUSTERED ([Id]) ");
4354
sql.AppendLine("WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ");
4455
sql.AppendLine("ON [PRIMARY];");

0 commit comments

Comments
 (0)