WIP
This commit is contained in:
parent
71576a4b1f
commit
73a1f11e99
21 changed files with 236 additions and 228 deletions
10
Core/Sql/ConnectionFactory/IDbConnectionFactory.cs
Normal file
10
Core/Sql/ConnectionFactory/IDbConnectionFactory.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
namespace PlanTempus.Core.Sql.ConnectionFactory
|
||||
{
|
||||
public record ConnectionStringParameters(string user, string pwd);
|
||||
|
||||
public interface IDbConnectionFactory
|
||||
{
|
||||
System.Data.IDbConnection Create();
|
||||
System.Data.IDbConnection Create(ConnectionStringParameters connectionStringTemplateParameters);
|
||||
}
|
||||
}
|
||||
65
Core/Sql/ConnectionFactory/PostgresConnectionFactory.cs
Normal file
65
Core/Sql/ConnectionFactory/PostgresConnectionFactory.cs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
using Npgsql;
|
||||
using System.Data;
|
||||
namespace PlanTempus.Core.Sql.ConnectionFactory
|
||||
{
|
||||
|
||||
|
||||
public class PostgresConnectionFactory : IDbConnectionFactory, IAsyncDisposable
|
||||
{
|
||||
private readonly NpgsqlDataSource _baseDataSource;
|
||||
private readonly Action<NpgsqlDataSourceBuilder> _configureDataSource;
|
||||
private readonly Microsoft.Extensions.Logging.ILoggerFactory _loggerFactory; //this is not tested nor implemented, I just created it as an idea
|
||||
|
||||
public PostgresConnectionFactory(
|
||||
string connectionString,
|
||||
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory = null,
|
||||
Action<NpgsqlDataSourceBuilder> configureDataSource = null)
|
||||
{
|
||||
_loggerFactory = loggerFactory;
|
||||
_configureDataSource = configureDataSource ?? (builder => { });
|
||||
|
||||
// Opret base data source med konfiguration
|
||||
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
|
||||
ConfigureDataSourceBuilder(dataSourceBuilder);
|
||||
_baseDataSource = dataSourceBuilder.Build();
|
||||
}
|
||||
|
||||
public IDbConnection Create()
|
||||
{
|
||||
return _baseDataSource.CreateConnection();
|
||||
}
|
||||
|
||||
public IDbConnection Create(ConnectionStringParameters param)
|
||||
{
|
||||
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(
|
||||
_baseDataSource.ConnectionString)
|
||||
{
|
||||
Username = param.user,
|
||||
Password = param.pwd
|
||||
};
|
||||
|
||||
var tempDataSourceBuilder = new NpgsqlDataSourceBuilder(
|
||||
connectionStringBuilder.ToString());
|
||||
|
||||
ConfigureDataSourceBuilder(tempDataSourceBuilder);
|
||||
|
||||
var tempDataSource = tempDataSourceBuilder.Build();
|
||||
return tempDataSource.CreateConnection();
|
||||
}
|
||||
|
||||
private void ConfigureDataSourceBuilder(NpgsqlDataSourceBuilder builder)
|
||||
{
|
||||
if (_loggerFactory != null)
|
||||
{
|
||||
builder.UseLoggerFactory(_loggerFactory);
|
||||
}
|
||||
|
||||
_configureDataSource?.Invoke(builder);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await _baseDataSource.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
100
Core/Sql/SqlOperations.cs
Normal file
100
Core/Sql/SqlOperations.cs
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
using System.Data;
|
||||
|
||||
namespace PlanTempus.Core.Sql
|
||||
{
|
||||
public class DatabaseScope : IDisposable
|
||||
{
|
||||
private readonly IDbConnection _connection;
|
||||
private readonly IOperationHolder<DependencyTelemetry> _operation;
|
||||
|
||||
public DatabaseScope(IDbConnection connection, IOperationHolder<DependencyTelemetry> operation)
|
||||
{
|
||||
_connection = connection;
|
||||
_operation = operation;
|
||||
}
|
||||
|
||||
public IDbConnection Connection => _connection;
|
||||
|
||||
public void Success()
|
||||
{
|
||||
_operation.Telemetry.Success = true;
|
||||
}
|
||||
|
||||
public void Error(Exception ex)
|
||||
{
|
||||
_operation.Telemetry.Success = false;
|
||||
_operation.Telemetry.Properties["Error"] = ex.Message;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_operation.Dispose();
|
||||
_connection.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IDatabaseOperations
|
||||
{
|
||||
DatabaseScope CreateScope(string operationName);
|
||||
Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName);
|
||||
Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName);
|
||||
}
|
||||
|
||||
public class SqlOperations : IDatabaseOperations
|
||||
{
|
||||
private readonly IDbConnectionFactory _connectionFactory;
|
||||
private readonly TelemetryClient _telemetryClient;
|
||||
|
||||
public SqlOperations(IDbConnectionFactory connectionFactory, TelemetryClient telemetryClient)
|
||||
{
|
||||
_connectionFactory = connectionFactory;
|
||||
_telemetryClient = telemetryClient;
|
||||
}
|
||||
|
||||
public DatabaseScope CreateScope(string operationName)
|
||||
{
|
||||
var connection = _connectionFactory.Create();
|
||||
var operation = _telemetryClient.StartOperation<DependencyTelemetry>(operationName);
|
||||
operation.Telemetry.Type = "SQL";
|
||||
operation.Telemetry.Target = "PostgreSQL";
|
||||
|
||||
return new DatabaseScope(connection, operation);
|
||||
}
|
||||
|
||||
public async Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName)
|
||||
{
|
||||
using var scope = CreateScope(operationName);
|
||||
try
|
||||
{
|
||||
var result = await operation(scope.Connection);
|
||||
scope.Success();
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
scope.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName)
|
||||
{
|
||||
using var scope = CreateScope(operationName);
|
||||
try
|
||||
{
|
||||
await operation(scope.Connection);
|
||||
scope.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
scope.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
using Insight.Database;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
using PlanTempus.Database.Core;
|
||||
using PlanTempus.Database.Core.ConnectionFactory;
|
||||
using System.Data;
|
||||
|
||||
namespace PlanTempus.Database.ConfigurationManagementSystem;
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
namespace PlanTempus.Database.Core.ConnectionFactory
|
||||
{
|
||||
public record ConnectionStringParameters(string user, string pwd);
|
||||
|
||||
public interface IDbConnectionFactory
|
||||
{
|
||||
System.Data.IDbConnection Create();
|
||||
System.Data.IDbConnection Create(ConnectionStringParameters connectionStringTemplateParameters);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
using Npgsql;
|
||||
using System.Data;
|
||||
namespace PlanTempus.Database.Core.ConnectionFactory
|
||||
{
|
||||
|
||||
|
||||
public class PostgresConnectionFactory : IDbConnectionFactory, IAsyncDisposable
|
||||
{
|
||||
private readonly NpgsqlDataSource _baseDataSource;
|
||||
private readonly Action<NpgsqlDataSourceBuilder> _configureDataSource;
|
||||
private readonly Microsoft.Extensions.Logging.ILoggerFactory _loggerFactory; //this is not tested nor implemented, I just created it as an idea
|
||||
|
||||
public PostgresConnectionFactory(
|
||||
string connectionString,
|
||||
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory = null,
|
||||
Action<NpgsqlDataSourceBuilder> configureDataSource = null)
|
||||
{
|
||||
_loggerFactory = loggerFactory;
|
||||
_configureDataSource = configureDataSource ?? (builder => { });
|
||||
|
||||
// Opret base data source med konfiguration
|
||||
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
|
||||
ConfigureDataSourceBuilder(dataSourceBuilder);
|
||||
_baseDataSource = dataSourceBuilder.Build();
|
||||
}
|
||||
|
||||
public IDbConnection Create()
|
||||
{
|
||||
return _baseDataSource.CreateConnection();
|
||||
}
|
||||
|
||||
public IDbConnection Create(ConnectionStringParameters param)
|
||||
{
|
||||
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(
|
||||
_baseDataSource.ConnectionString)
|
||||
{
|
||||
Username = param.user,
|
||||
Password = param.pwd
|
||||
};
|
||||
|
||||
var tempDataSourceBuilder = new NpgsqlDataSourceBuilder(
|
||||
connectionStringBuilder.ToString());
|
||||
|
||||
ConfigureDataSourceBuilder(tempDataSourceBuilder);
|
||||
|
||||
var tempDataSource = tempDataSourceBuilder.Build();
|
||||
return tempDataSource.CreateConnection();
|
||||
}
|
||||
|
||||
private void ConfigureDataSourceBuilder(NpgsqlDataSourceBuilder builder)
|
||||
{
|
||||
if (_loggerFactory != null)
|
||||
{
|
||||
builder.UseLoggerFactory(_loggerFactory);
|
||||
}
|
||||
|
||||
_configureDataSource?.Invoke(builder);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await _baseDataSource.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
using System.Data;
|
||||
using Insight.Database;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
using PlanTempus.Database.Common;
|
||||
using PlanTempus.Database.Core.ConnectionFactory;
|
||||
|
||||
namespace PlanTempus.Database.Core.DCL
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
using System.Data;
|
||||
using Insight.Database;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
using PlanTempus.Database.Common;
|
||||
using PlanTempus.Database.Core;
|
||||
using PlanTempus.Database.Core.ConnectionFactory;
|
||||
|
||||
namespace PlanTempus.Database.Core.DCL
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
using System.Data;
|
||||
using Insight.Database;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
using PlanTempus.Database.Common;
|
||||
using PlanTempus.Database.Core;
|
||||
using PlanTempus.Database.Core.ConnectionFactory;
|
||||
|
||||
namespace PlanTempus.Database.Core.DCL
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
using Insight.Database;
|
||||
using PlanTempus.Database.Core.ConnectionFactory;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
using System.Data;
|
||||
|
||||
namespace PlanTempus.Database.Core.DDL
|
||||
|
|
@ -89,7 +89,6 @@ namespace PlanTempus.Database.Core.DDL
|
|||
);";
|
||||
|
||||
db.ExecuteSql(sql);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
namespace PlanTempus.Database.Core
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
|
||||
namespace PlanTempus.Database.Core
|
||||
{
|
||||
public interface IDbConfigure<T>
|
||||
{
|
||||
void With(T command, ConnectionFactory.ConnectionStringParameters parameters = null);
|
||||
void With(T command, ConnectionStringParameters parameters = null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,100 +0,0 @@
|
|||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using PlanTempus.Database.ModuleRegistry;
|
||||
using System.Data;
|
||||
|
||||
namespace PlanTempus.Database.Core.Sql
|
||||
{
|
||||
public class DatabaseScope : IDisposable
|
||||
{
|
||||
private readonly IDbConnection _connection;
|
||||
private readonly IOperationHolder<DependencyTelemetry> _operation;
|
||||
|
||||
public DatabaseScope(IDbConnection connection, IOperationHolder<DependencyTelemetry> operation)
|
||||
{
|
||||
_connection = connection;
|
||||
_operation = operation;
|
||||
}
|
||||
|
||||
public IDbConnection Connection => _connection;
|
||||
|
||||
public void Success()
|
||||
{
|
||||
_operation.Telemetry.Success = true;
|
||||
}
|
||||
|
||||
public void Error(Exception ex)
|
||||
{
|
||||
_operation.Telemetry.Success = false;
|
||||
_operation.Telemetry.Properties["Error"] = ex.Message;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_operation.Dispose();
|
||||
_connection.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IDatabaseOperations
|
||||
{
|
||||
DatabaseScope CreateScope(string operationName);
|
||||
Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName);
|
||||
Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName);
|
||||
}
|
||||
|
||||
public class SqlOperations : IDatabaseOperations
|
||||
{
|
||||
private readonly ConnectionFactory.IDbConnectionFactory _connectionFactory;
|
||||
private readonly TelemetryClient _telemetryClient;
|
||||
|
||||
public SqlOperations(ConnectionFactory.IDbConnectionFactory connectionFactory, TelemetryClient telemetryClient)
|
||||
{
|
||||
_connectionFactory = connectionFactory;
|
||||
_telemetryClient = telemetryClient;
|
||||
}
|
||||
|
||||
public DatabaseScope CreateScope(string operationName)
|
||||
{
|
||||
var connection = _connectionFactory.Create();
|
||||
var operation = _telemetryClient.StartOperation<DependencyTelemetry>(operationName);
|
||||
operation.Telemetry.Type = "SQL";
|
||||
operation.Telemetry.Target = "PostgreSQL";
|
||||
|
||||
return new DatabaseScope(connection, operation);
|
||||
}
|
||||
|
||||
public async Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName)
|
||||
{
|
||||
using var scope = CreateScope(operationName);
|
||||
try
|
||||
{
|
||||
var result = await operation(scope.Connection);
|
||||
scope.Success();
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
scope.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName)
|
||||
{
|
||||
using var scope = CreateScope(operationName);
|
||||
try
|
||||
{
|
||||
await operation(scope.Connection);
|
||||
scope.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
scope.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
using Autofac;
|
||||
using Npgsql;
|
||||
using System.Data;
|
||||
using PlanTempus.Core.Sql;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
|
||||
namespace PlanTempus.Database.ModuleRegistry
|
||||
{
|
||||
|
||||
|
|
@ -12,13 +13,13 @@ namespace PlanTempus.Database.ModuleRegistry
|
|||
{
|
||||
Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider();
|
||||
|
||||
builder.RegisterType<Core.ConnectionFactory.PostgresConnectionFactory>()
|
||||
.As<Core.ConnectionFactory.IDbConnectionFactory>()
|
||||
builder.RegisterType<PostgresConnectionFactory>()
|
||||
.As<IDbConnectionFactory>()
|
||||
.WithParameter(new TypedParameter(typeof(string), ConnectionString))
|
||||
.SingleInstance();
|
||||
|
||||
builder.RegisterType<Core.Sql.SqlOperations>()
|
||||
.As<Core.Sql.IDatabaseOperations>();
|
||||
builder.RegisterType<SqlOperations>()
|
||||
.As<IDatabaseOperations>();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Insight.Database;
|
||||
using PlanTempus.Core.Sql;
|
||||
|
||||
namespace PlanTempus.Components.Organizations.Create
|
||||
{
|
||||
|
|
@ -20,8 +21,9 @@ namespace PlanTempus.Components.Organizations.Create
|
|||
var now = DateTime.UtcNow;
|
||||
|
||||
var sql = @"
|
||||
INSERT INTO Organizations (Id, Name, Description, CreatedById, CreatedAt, UpdatedAt)
|
||||
VALUES (@Id, @Name, @Description, @CreatedById, @CreatedAt, @UpdatedAt)";
|
||||
INSERT INTO organizations (id, name, description, created_by_id)
|
||||
VALUES (@Id, @Name, @Description, @CreatedById)";
|
||||
|
||||
|
||||
await db.Connection.ExecuteSqlAsync(sql, new
|
||||
{
|
||||
|
|
|
|||
|
|
@ -10,6 +10,9 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Users\Create\" />
|
||||
<Folder Include="Users\Delete\" />
|
||||
<Folder Include="Users\Update\" />
|
||||
<Folder Include="Organizations\Delete\" />
|
||||
<Folder Include="Organizations\Update\" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ using Insight.Database;
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Npgsql;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
using PlanTempus.Database.ConfigurationManagementSystem;
|
||||
using PlanTempus.Database.Core.ConnectionFactory;
|
||||
using PlanTempus.Database.Core.DCL;
|
||||
using PlanTempus.Database.Core.DDL;
|
||||
using System.Data;
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ using System.Data;
|
|||
using Newtonsoft.Json;
|
||||
using Autofac;
|
||||
using Shouldly;
|
||||
using PlanTempus.Database.Core.ConnectionFactory;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
|
||||
namespace PlanTempus.Tests.ConfigurationSystem;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ using Insight.Database;
|
|||
using PlanTempus.Core.Configurations;
|
||||
using PlanTempus.Core.Configurations.JsonConfigProvider;
|
||||
using PlanTempus.Core.Configurations.SmartConfigProvider;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
|
||||
namespace PlanTempus.Tests.ConfigurationTests
|
||||
{
|
||||
|
|
@ -66,7 +67,7 @@ namespace PlanTempus.Tests.ConfigurationTests
|
|||
[TestMethod]
|
||||
public void TryGetActiveConfigurations()
|
||||
{
|
||||
var connFactory = Container.Resolve<Database.Core.ConnectionFactory.IDbConnectionFactory>();
|
||||
var connFactory = Container.Resolve<IDbConnectionFactory>();
|
||||
|
||||
const string sql = @"
|
||||
SELECT id, ""key"", value, label, content_type,
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ using Autofac;
|
|||
using System.Data;
|
||||
using Insight.Database;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using PlanTempus.Database.Core.Sql;
|
||||
using PlanTempus.Database.Core.ConnectionFactory;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
using PlanTempus.Core.Sql;
|
||||
|
||||
namespace PlanTempus.Tests
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue