Extract Database to interface
This commit is contained in:
@@ -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>
|
||||
{
|
||||
|
20
PluralKit.Core/Database/DatabaseExt.cs
Normal file
20
PluralKit.Core/Database/DatabaseExt.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
9
PluralKit.Core/Database/IDatabase.cs
Normal file
9
PluralKit.Core/Database/IDatabase.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public interface IDatabase
|
||||
{
|
||||
Task<IPKConnection> Obtain();
|
||||
}
|
||||
}
|
@@ -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>();
|
||||
|
@@ -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;
|
||||
|
@@ -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; }
|
||||
|
@@ -8,7 +8,7 @@ using Npgsql;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public class PKTransaction: DbTransaction, IPKTransaction
|
||||
internal class PKTransaction: DbTransaction, IPKTransaction
|
||||
{
|
||||
public NpgsqlTransaction Inner { get; }
|
||||
|
||||
|
Reference in New Issue
Block a user