Extract Database to interface

This commit is contained in:
Ske
2020-06-13 19:36:43 +02:00
parent 70df9cd893
commit 90ac186183
27 changed files with 76 additions and 54 deletions

View File

@@ -14,7 +14,7 @@ using Serilog;
namespace PluralKit.Core
{
public class Database
internal class Database: IDatabase
{
private readonly CoreConfig _config;
private readonly ILogger _logger;
@@ -64,18 +64,6 @@ namespace PluralKit.Core
await conn.OpenAsync();
return conn;
}
public async Task Execute(Func<IPKConnection, Task> func)
{
await using var conn = await Obtain();
await func(conn);
}
public async Task<T> Execute<T>(Func<IPKConnection, Task<T>> func)
{
await using var conn = await Obtain();
return await func(conn);
}
private class PassthroughTypeHandler<T>: SqlMapper.TypeHandler<T>
{

View File

@@ -0,0 +1,20 @@
using System;
using System.Threading.Tasks;
namespace PluralKit.Core
{
public static class DatabaseExt
{
public static async Task Execute(this IDatabase db, Func<IPKConnection, Task> func)
{
await using var conn = await db.Obtain();
await func(conn);
}
public static async Task<T> Execute<T>(this IDatabase db, Func<IPKConnection, Task<T>> func)
{
await using var conn = await db.Obtain();
return await func(conn);
}
}
}

View File

@@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace PluralKit.Core
{
public interface IDatabase
{
Task<IPKConnection> Obtain();
}
}

View File

@@ -16,10 +16,10 @@ namespace PluralKit.Core
private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files
private const int TargetSchemaVersion = 7;
private Database _conn;
private IDatabase _conn;
private ILogger _logger;
public Schemas(Database conn, ILogger logger)
public Schemas(IDatabase conn, ILogger logger)
{
_conn = conn;
_logger = logger.ForContext<Schemas>();

View File

@@ -15,9 +15,9 @@ using Serilog;
namespace PluralKit.Core
{
public class PKCommand: DbCommand, IPKCommand
internal class PKCommand: DbCommand, IPKCommand
{
public NpgsqlCommand Inner { get; }
private NpgsqlCommand Inner { get; }
private readonly PKConnection _ourConnection;
private readonly ILogger _logger;

View File

@@ -15,7 +15,7 @@ using Serilog;
namespace PluralKit.Core
{
public class PKConnection: DbConnection, IPKConnection
internal class PKConnection: DbConnection, IPKConnection
{
public NpgsqlConnection Inner { get; }
public Guid ConnectionId { get; }

View File

@@ -8,7 +8,7 @@ using Npgsql;
namespace PluralKit.Core
{
public class PKTransaction: DbTransaction, IPKTransaction
internal class PKTransaction: DbTransaction, IPKTransaction
{
public NpgsqlTransaction Inner { get; }