From 90ac1861837a42916e63d1f2891f0cf2e730be48 Mon Sep 17 00:00:00 2001 From: Ske Date: Sat, 13 Jun 2020 19:36:43 +0200 Subject: [PATCH] Extract Database to interface --- PluralKit.API/Controllers/SystemController.cs | 4 ++-- PluralKit.API/Program.cs | 2 +- PluralKit.Bot/Commands/Autoproxy.cs | 4 ++-- PluralKit.Bot/Commands/MemberAvatar.cs | 4 ++-- PluralKit.Bot/Commands/MemberEdit.cs | 4 ++-- PluralKit.Bot/Commands/ServerConfig.cs | 4 ++-- PluralKit.Bot/Commands/SystemEdit.cs | 4 ++-- PluralKit.Bot/Commands/SystemList.cs | 4 ++-- PluralKit.Bot/Handlers/MessageCreated.cs | 4 ++-- PluralKit.Bot/Handlers/MessageEdited.cs | 4 ++-- PluralKit.Bot/Init.cs | 2 +- PluralKit.Bot/Proxy/ProxyService.cs | 4 ++-- PluralKit.Bot/Services/EmbedService.cs | 4 ++-- PluralKit.Bot/Services/LogChannelService.cs | 4 ++-- PluralKit.Bot/Services/LoggerCleanService.cs | 4 ++-- .../Services/PeriodicStatCollector.cs | 4 ++-- PluralKit.Core/Database/Database.cs | 14 +------------ PluralKit.Core/Database/DatabaseExt.cs | 20 +++++++++++++++++++ PluralKit.Core/Database/IDatabase.cs | 9 +++++++++ PluralKit.Core/Database/Schemas.cs | 4 ++-- PluralKit.Core/Database/Wrappers/PKCommand.cs | 4 ++-- .../Database/Wrappers/PKConnection.cs | 2 +- .../Database/Wrappers/PKTransaction.cs | 2 +- PluralKit.Core/Services/DataFileService.cs | 4 ++-- PluralKit.Core/Services/PostgresDataStore.cs | 4 ++-- PluralKit.Core/Utils/ConnectionUtils.cs | 2 +- PluralKit.Core/Utils/InitUtils.cs | 5 +++++ 27 files changed, 76 insertions(+), 54 deletions(-) create mode 100644 PluralKit.Core/Database/DatabaseExt.cs create mode 100644 PluralKit.Core/Database/IDatabase.cs diff --git a/PluralKit.API/Controllers/SystemController.cs b/PluralKit.API/Controllers/SystemController.cs index 05bc4223..4d3bd90a 100644 --- a/PluralKit.API/Controllers/SystemController.cs +++ b/PluralKit.API/Controllers/SystemController.cs @@ -40,10 +40,10 @@ namespace PluralKit.API public class SystemController : ControllerBase { private IDataStore _data; - private Database _conn; + private IDatabase _conn; private TokenAuthService _auth; - public SystemController(IDataStore data, Database conn, TokenAuthService auth) + public SystemController(IDataStore data, IDatabase conn, TokenAuthService auth) { _data = data; _conn = conn; diff --git a/PluralKit.API/Program.cs b/PluralKit.API/Program.cs index e3946709..50f0d571 100644 --- a/PluralKit.API/Program.cs +++ b/PluralKit.API/Program.cs @@ -11,7 +11,7 @@ namespace PluralKit.API { public static void Main(string[] args) { - Database.InitStatic(); + InitUtils.InitStatic(); CreateHostBuilder(args).Build().Run(); } diff --git a/PluralKit.Bot/Commands/Autoproxy.cs b/PluralKit.Bot/Commands/Autoproxy.cs index a89e9e9f..3f871829 100644 --- a/PluralKit.Bot/Commands/Autoproxy.cs +++ b/PluralKit.Bot/Commands/Autoproxy.cs @@ -11,9 +11,9 @@ namespace PluralKit.Bot { public class Autoproxy { - private readonly Database _db; + private readonly IDatabase _db; - public Autoproxy(Database db) + public Autoproxy(IDatabase db) { _db = db; } diff --git a/PluralKit.Bot/Commands/MemberAvatar.cs b/PluralKit.Bot/Commands/MemberAvatar.cs index 1ddb8266..9be5e90f 100644 --- a/PluralKit.Bot/Commands/MemberAvatar.cs +++ b/PluralKit.Bot/Commands/MemberAvatar.cs @@ -14,9 +14,9 @@ namespace PluralKit.Bot { public class MemberAvatar { - private readonly Database _db; + private readonly IDatabase _db; - public MemberAvatar(Database db) + public MemberAvatar(IDatabase db) { _db = db; } diff --git a/PluralKit.Bot/Commands/MemberEdit.cs b/PluralKit.Bot/Commands/MemberEdit.cs index 2fc6019d..a79f2f96 100644 --- a/PluralKit.Bot/Commands/MemberEdit.cs +++ b/PluralKit.Bot/Commands/MemberEdit.cs @@ -12,9 +12,9 @@ namespace PluralKit.Bot public class MemberEdit { private readonly IDataStore _data; - private readonly Database _db; + private readonly IDatabase _db; - public MemberEdit(IDataStore data, Database db) + public MemberEdit(IDataStore data, IDatabase db) { _data = data; _db = db; diff --git a/PluralKit.Bot/Commands/ServerConfig.cs b/PluralKit.Bot/Commands/ServerConfig.cs index 95e63e85..cc7f7daf 100644 --- a/PluralKit.Bot/Commands/ServerConfig.cs +++ b/PluralKit.Bot/Commands/ServerConfig.cs @@ -13,9 +13,9 @@ namespace PluralKit.Bot { public class ServerConfig { - private Database _db; + private IDatabase _db; private LoggerCleanService _cleanService; - public ServerConfig(LoggerCleanService cleanService, Database db) + public ServerConfig(LoggerCleanService cleanService, IDatabase db) { _cleanService = cleanService; _db = db; diff --git a/PluralKit.Bot/Commands/SystemEdit.cs b/PluralKit.Bot/Commands/SystemEdit.cs index 798bb5c5..477a9122 100644 --- a/PluralKit.Bot/Commands/SystemEdit.cs +++ b/PluralKit.Bot/Commands/SystemEdit.cs @@ -18,10 +18,10 @@ namespace PluralKit.Bot public class SystemEdit { private IDataStore _data; - private Database _db; + private IDatabase _db; private EmbedService _embeds; - public SystemEdit(IDataStore data, EmbedService embeds, Database db) + public SystemEdit(IDataStore data, EmbedService embeds, IDatabase db) { _data = data; _embeds = embeds; diff --git a/PluralKit.Bot/Commands/SystemList.cs b/PluralKit.Bot/Commands/SystemList.cs index 64e1bbec..f225b457 100644 --- a/PluralKit.Bot/Commands/SystemList.cs +++ b/PluralKit.Bot/Commands/SystemList.cs @@ -16,10 +16,10 @@ namespace PluralKit.Bot public class SystemList { private readonly IClock _clock; - private readonly Database _db; + private readonly IDatabase _db; private readonly ILogger _logger; - public SystemList(Database db, ILogger logger, IClock clock) + public SystemList(IDatabase db, ILogger logger, IClock clock) { _db = db; _logger = logger; diff --git a/PluralKit.Bot/Handlers/MessageCreated.cs b/PluralKit.Bot/Handlers/MessageCreated.cs index 87333b0f..ec3ebd39 100644 --- a/PluralKit.Bot/Handlers/MessageCreated.cs +++ b/PluralKit.Bot/Handlers/MessageCreated.cs @@ -22,12 +22,12 @@ namespace PluralKit.Bot private readonly IMetrics _metrics; private readonly ProxyService _proxy; private readonly ILifetimeScope _services; - private readonly Database _db; + private readonly IDatabase _db; private readonly IDataStore _data; public MessageCreated(LastMessageCacheService lastMessageCache, LoggerCleanService loggerClean, IMetrics metrics, ProxyService proxy, DiscordShardedClient client, - CommandTree tree, ILifetimeScope services, Database db, IDataStore data) + CommandTree tree, ILifetimeScope services, IDatabase db, IDataStore data) { _lastMessageCache = lastMessageCache; _loggerClean = loggerClean; diff --git a/PluralKit.Bot/Handlers/MessageEdited.cs b/PluralKit.Bot/Handlers/MessageEdited.cs index b1423f3d..f1a3dca4 100644 --- a/PluralKit.Bot/Handlers/MessageEdited.cs +++ b/PluralKit.Bot/Handlers/MessageEdited.cs @@ -11,9 +11,9 @@ namespace PluralKit.Bot { private readonly LastMessageCacheService _lastMessageCache; private readonly ProxyService _proxy; - private readonly Database _db; + private readonly IDatabase _db; - public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, Database db) + public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db) { _lastMessageCache = lastMessageCache; _proxy = proxy; diff --git a/PluralKit.Bot/Init.cs b/PluralKit.Bot/Init.cs index 1df8b4dd..7e72b3fe 100644 --- a/PluralKit.Bot/Init.cs +++ b/PluralKit.Bot/Init.cs @@ -21,7 +21,7 @@ namespace PluralKit.Bot { // Load configuration and run global init stuff var config = InitUtils.BuildConfiguration(args).Build(); - Database.InitStatic(); + InitUtils.InitStatic(); // Set up DI container and modules var services = BuildContainer(config); diff --git a/PluralKit.Bot/Proxy/ProxyService.cs b/PluralKit.Bot/Proxy/ProxyService.cs index 3c1844c1..9c8b1cb1 100644 --- a/PluralKit.Bot/Proxy/ProxyService.cs +++ b/PluralKit.Bot/Proxy/ProxyService.cs @@ -21,14 +21,14 @@ namespace PluralKit.Bot public static readonly TimeSpan MessageDeletionDelay = TimeSpan.FromMilliseconds(1000); private readonly LogChannelService _logChannel; - private readonly Database _db; + private readonly IDatabase _db; private readonly IDataStore _data; private readonly ILogger _logger; private readonly WebhookExecutorService _webhookExecutor; private readonly ProxyMatcher _matcher; public ProxyService(LogChannelService logChannel, IDataStore data, ILogger logger, - WebhookExecutorService webhookExecutor, Database db, ProxyMatcher matcher) + WebhookExecutorService webhookExecutor, IDatabase db, ProxyMatcher matcher) { _logChannel = logChannel; _data = data; diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index 7e469b9f..4b96d5f7 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -16,10 +16,10 @@ namespace PluralKit.Bot { public class EmbedService { private IDataStore _data; - private Database _db; + private IDatabase _db; private DiscordShardedClient _client; - public EmbedService(DiscordShardedClient client, IDataStore data, Database db) + public EmbedService(DiscordShardedClient client, IDataStore data, IDatabase db) { _client = client; _data = data; diff --git a/PluralKit.Bot/Services/LogChannelService.cs b/PluralKit.Bot/Services/LogChannelService.cs index 93afafeb..e6f19f8a 100644 --- a/PluralKit.Bot/Services/LogChannelService.cs +++ b/PluralKit.Bot/Services/LogChannelService.cs @@ -14,12 +14,12 @@ using Serilog; namespace PluralKit.Bot { public class LogChannelService { private readonly EmbedService _embed; - private readonly Database _db; + private readonly IDatabase _db; private readonly IDataStore _data; private readonly ILogger _logger; private readonly DiscordRestClient _rest; - public LogChannelService(EmbedService embed, ILogger logger, DiscordRestClient rest, Database db, IDataStore data) + public LogChannelService(EmbedService embed, ILogger logger, DiscordRestClient rest, IDatabase db, IDataStore data) { _embed = embed; _rest = rest; diff --git a/PluralKit.Bot/Services/LoggerCleanService.cs b/PluralKit.Bot/Services/LoggerCleanService.cs index a4d9fdf3..8ddc855e 100644 --- a/PluralKit.Bot/Services/LoggerCleanService.cs +++ b/PluralKit.Bot/Services/LoggerCleanService.cs @@ -53,10 +53,10 @@ namespace PluralKit.Bot .Where(b => b.WebhookName != null) .ToDictionary(b => b.WebhookName); - private Database _db; + private IDatabase _db; private DiscordShardedClient _client; - public LoggerCleanService(Database db, DiscordShardedClient client) + public LoggerCleanService(IDatabase db, DiscordShardedClient client) { _db = db; _client = client; diff --git a/PluralKit.Bot/Services/PeriodicStatCollector.cs b/PluralKit.Bot/Services/PeriodicStatCollector.cs index 4e2a3ff0..b96770e9 100644 --- a/PluralKit.Bot/Services/PeriodicStatCollector.cs +++ b/PluralKit.Bot/Services/PeriodicStatCollector.cs @@ -23,7 +23,7 @@ namespace PluralKit.Bot private IMetrics _metrics; private CpuStatService _cpu; - private Database _db; + private IDatabase _db; private WebhookCacheService _webhookCache; @@ -31,7 +31,7 @@ namespace PluralKit.Bot private ILogger _logger; - public PeriodicStatCollector(DiscordShardedClient client, IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, CpuStatService cpu, Database db) + public PeriodicStatCollector(DiscordShardedClient client, IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, CpuStatService cpu, IDatabase db) { _client = client; _metrics = metrics; diff --git a/PluralKit.Core/Database/Database.cs b/PluralKit.Core/Database/Database.cs index b8328019..ae669f9d 100644 --- a/PluralKit.Core/Database/Database.cs +++ b/PluralKit.Core/Database/Database.cs @@ -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 func) - { - await using var conn = await Obtain(); - await func(conn); - } - - public async Task Execute(Func> func) - { - await using var conn = await Obtain(); - return await func(conn); - } private class PassthroughTypeHandler: SqlMapper.TypeHandler { diff --git a/PluralKit.Core/Database/DatabaseExt.cs b/PluralKit.Core/Database/DatabaseExt.cs new file mode 100644 index 00000000..afae07f7 --- /dev/null +++ b/PluralKit.Core/Database/DatabaseExt.cs @@ -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 func) + { + await using var conn = await db.Obtain(); + await func(conn); + } + + public static async Task Execute(this IDatabase db, Func> func) + { + await using var conn = await db.Obtain(); + return await func(conn); + } + } +} \ No newline at end of file diff --git a/PluralKit.Core/Database/IDatabase.cs b/PluralKit.Core/Database/IDatabase.cs new file mode 100644 index 00000000..3cb44af0 --- /dev/null +++ b/PluralKit.Core/Database/IDatabase.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace PluralKit.Core +{ + public interface IDatabase + { + Task Obtain(); + } +} \ No newline at end of file diff --git a/PluralKit.Core/Database/Schemas.cs b/PluralKit.Core/Database/Schemas.cs index a0b53a02..ef11ccd3 100644 --- a/PluralKit.Core/Database/Schemas.cs +++ b/PluralKit.Core/Database/Schemas.cs @@ -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(); diff --git a/PluralKit.Core/Database/Wrappers/PKCommand.cs b/PluralKit.Core/Database/Wrappers/PKCommand.cs index aa21241a..02d75079 100644 --- a/PluralKit.Core/Database/Wrappers/PKCommand.cs +++ b/PluralKit.Core/Database/Wrappers/PKCommand.cs @@ -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; diff --git a/PluralKit.Core/Database/Wrappers/PKConnection.cs b/PluralKit.Core/Database/Wrappers/PKConnection.cs index 153a7f02..135d26cc 100644 --- a/PluralKit.Core/Database/Wrappers/PKConnection.cs +++ b/PluralKit.Core/Database/Wrappers/PKConnection.cs @@ -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; } diff --git a/PluralKit.Core/Database/Wrappers/PKTransaction.cs b/PluralKit.Core/Database/Wrappers/PKTransaction.cs index 84c17722..50b83119 100644 --- a/PluralKit.Core/Database/Wrappers/PKTransaction.cs +++ b/PluralKit.Core/Database/Wrappers/PKTransaction.cs @@ -8,7 +8,7 @@ using Npgsql; namespace PluralKit.Core { - public class PKTransaction: DbTransaction, IPKTransaction + internal class PKTransaction: DbTransaction, IPKTransaction { public NpgsqlTransaction Inner { get; } diff --git a/PluralKit.Core/Services/DataFileService.cs b/PluralKit.Core/Services/DataFileService.cs index 6cd7cb87..7eb889c0 100644 --- a/PluralKit.Core/Services/DataFileService.cs +++ b/PluralKit.Core/Services/DataFileService.cs @@ -15,10 +15,10 @@ namespace PluralKit.Core public class DataFileService { private IDataStore _data; - private Database _db; + private IDatabase _db; private ILogger _logger; - public DataFileService(ILogger logger, IDataStore data, Database db) + public DataFileService(ILogger logger, IDataStore data, IDatabase db) { _data = data; _db = db; diff --git a/PluralKit.Core/Services/PostgresDataStore.cs b/PluralKit.Core/Services/PostgresDataStore.cs index b8f7e9b8..a9c57dc6 100644 --- a/PluralKit.Core/Services/PostgresDataStore.cs +++ b/PluralKit.Core/Services/PostgresDataStore.cs @@ -10,10 +10,10 @@ using Serilog; namespace PluralKit.Core { public class PostgresDataStore: IDataStore { - private Database _conn; + private IDatabase _conn; private ILogger _logger; - public PostgresDataStore(Database conn, ILogger logger) + public PostgresDataStore(IDatabase conn, ILogger logger) { _conn = conn; _logger = logger; diff --git a/PluralKit.Core/Utils/ConnectionUtils.cs b/PluralKit.Core/Utils/ConnectionUtils.cs index d133b8d5..3af6f2b9 100644 --- a/PluralKit.Core/Utils/ConnectionUtils.cs +++ b/PluralKit.Core/Utils/ConnectionUtils.cs @@ -7,7 +7,7 @@ using Dapper; namespace PluralKit.Core { public static class ConnectionUtils { - public static async IAsyncEnumerable QueryStreamAsync(this Database connFactory, string sql, object param) + public static async IAsyncEnumerable QueryStreamAsync(this IDatabase connFactory, string sql, object param) { await using var conn = await connFactory.Obtain(); diff --git a/PluralKit.Core/Utils/InitUtils.cs b/PluralKit.Core/Utils/InitUtils.cs index fd016ff5..38ef82f6 100644 --- a/PluralKit.Core/Utils/InitUtils.cs +++ b/PluralKit.Core/Utils/InitUtils.cs @@ -10,6 +10,11 @@ using NodaTime.Serialization.JsonNet; namespace PluralKit.Core { public static class InitUtils { + public static void InitStatic() + { + Database.InitStatic(); + } + public static IConfigurationBuilder BuildConfiguration(string[] args) => new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("pluralkit.conf", true)