Extract Database to interface
This commit is contained in:
parent
70df9cd893
commit
90ac186183
@ -40,10 +40,10 @@ namespace PluralKit.API
|
|||||||
public class SystemController : ControllerBase
|
public class SystemController : ControllerBase
|
||||||
{
|
{
|
||||||
private IDataStore _data;
|
private IDataStore _data;
|
||||||
private Database _conn;
|
private IDatabase _conn;
|
||||||
private TokenAuthService _auth;
|
private TokenAuthService _auth;
|
||||||
|
|
||||||
public SystemController(IDataStore data, Database conn, TokenAuthService auth)
|
public SystemController(IDataStore data, IDatabase conn, TokenAuthService auth)
|
||||||
{
|
{
|
||||||
_data = data;
|
_data = data;
|
||||||
_conn = conn;
|
_conn = conn;
|
||||||
|
@ -11,7 +11,7 @@ namespace PluralKit.API
|
|||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Database.InitStatic();
|
InitUtils.InitStatic();
|
||||||
CreateHostBuilder(args).Build().Run();
|
CreateHostBuilder(args).Build().Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,9 +11,9 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
public class Autoproxy
|
public class Autoproxy
|
||||||
{
|
{
|
||||||
private readonly Database _db;
|
private readonly IDatabase _db;
|
||||||
|
|
||||||
public Autoproxy(Database db)
|
public Autoproxy(IDatabase db)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,9 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
public class MemberAvatar
|
public class MemberAvatar
|
||||||
{
|
{
|
||||||
private readonly Database _db;
|
private readonly IDatabase _db;
|
||||||
|
|
||||||
public MemberAvatar(Database db)
|
public MemberAvatar(IDatabase db)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,9 @@ namespace PluralKit.Bot
|
|||||||
public class MemberEdit
|
public class MemberEdit
|
||||||
{
|
{
|
||||||
private readonly IDataStore _data;
|
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;
|
_data = data;
|
||||||
_db = db;
|
_db = db;
|
||||||
|
@ -13,9 +13,9 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
public class ServerConfig
|
public class ServerConfig
|
||||||
{
|
{
|
||||||
private Database _db;
|
private IDatabase _db;
|
||||||
private LoggerCleanService _cleanService;
|
private LoggerCleanService _cleanService;
|
||||||
public ServerConfig(LoggerCleanService cleanService, Database db)
|
public ServerConfig(LoggerCleanService cleanService, IDatabase db)
|
||||||
{
|
{
|
||||||
_cleanService = cleanService;
|
_cleanService = cleanService;
|
||||||
_db = db;
|
_db = db;
|
||||||
|
@ -18,10 +18,10 @@ namespace PluralKit.Bot
|
|||||||
public class SystemEdit
|
public class SystemEdit
|
||||||
{
|
{
|
||||||
private IDataStore _data;
|
private IDataStore _data;
|
||||||
private Database _db;
|
private IDatabase _db;
|
||||||
private EmbedService _embeds;
|
private EmbedService _embeds;
|
||||||
|
|
||||||
public SystemEdit(IDataStore data, EmbedService embeds, Database db)
|
public SystemEdit(IDataStore data, EmbedService embeds, IDatabase db)
|
||||||
{
|
{
|
||||||
_data = data;
|
_data = data;
|
||||||
_embeds = embeds;
|
_embeds = embeds;
|
||||||
|
@ -16,10 +16,10 @@ namespace PluralKit.Bot
|
|||||||
public class SystemList
|
public class SystemList
|
||||||
{
|
{
|
||||||
private readonly IClock _clock;
|
private readonly IClock _clock;
|
||||||
private readonly Database _db;
|
private readonly IDatabase _db;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
public SystemList(Database db, ILogger logger, IClock clock)
|
public SystemList(IDatabase db, ILogger logger, IClock clock)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
@ -22,12 +22,12 @@ namespace PluralKit.Bot
|
|||||||
private readonly IMetrics _metrics;
|
private readonly IMetrics _metrics;
|
||||||
private readonly ProxyService _proxy;
|
private readonly ProxyService _proxy;
|
||||||
private readonly ILifetimeScope _services;
|
private readonly ILifetimeScope _services;
|
||||||
private readonly Database _db;
|
private readonly IDatabase _db;
|
||||||
private readonly IDataStore _data;
|
private readonly IDataStore _data;
|
||||||
|
|
||||||
public MessageCreated(LastMessageCacheService lastMessageCache, LoggerCleanService loggerClean,
|
public MessageCreated(LastMessageCacheService lastMessageCache, LoggerCleanService loggerClean,
|
||||||
IMetrics metrics, ProxyService proxy, DiscordShardedClient client,
|
IMetrics metrics, ProxyService proxy, DiscordShardedClient client,
|
||||||
CommandTree tree, ILifetimeScope services, Database db, IDataStore data)
|
CommandTree tree, ILifetimeScope services, IDatabase db, IDataStore data)
|
||||||
{
|
{
|
||||||
_lastMessageCache = lastMessageCache;
|
_lastMessageCache = lastMessageCache;
|
||||||
_loggerClean = loggerClean;
|
_loggerClean = loggerClean;
|
||||||
|
@ -11,9 +11,9 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
private readonly LastMessageCacheService _lastMessageCache;
|
private readonly LastMessageCacheService _lastMessageCache;
|
||||||
private readonly ProxyService _proxy;
|
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;
|
_lastMessageCache = lastMessageCache;
|
||||||
_proxy = proxy;
|
_proxy = proxy;
|
||||||
|
@ -21,7 +21,7 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
// Load configuration and run global init stuff
|
// Load configuration and run global init stuff
|
||||||
var config = InitUtils.BuildConfiguration(args).Build();
|
var config = InitUtils.BuildConfiguration(args).Build();
|
||||||
Database.InitStatic();
|
InitUtils.InitStatic();
|
||||||
|
|
||||||
// Set up DI container and modules
|
// Set up DI container and modules
|
||||||
var services = BuildContainer(config);
|
var services = BuildContainer(config);
|
||||||
|
@ -21,14 +21,14 @@ namespace PluralKit.Bot
|
|||||||
public static readonly TimeSpan MessageDeletionDelay = TimeSpan.FromMilliseconds(1000);
|
public static readonly TimeSpan MessageDeletionDelay = TimeSpan.FromMilliseconds(1000);
|
||||||
|
|
||||||
private readonly LogChannelService _logChannel;
|
private readonly LogChannelService _logChannel;
|
||||||
private readonly Database _db;
|
private readonly IDatabase _db;
|
||||||
private readonly IDataStore _data;
|
private readonly IDataStore _data;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly WebhookExecutorService _webhookExecutor;
|
private readonly WebhookExecutorService _webhookExecutor;
|
||||||
private readonly ProxyMatcher _matcher;
|
private readonly ProxyMatcher _matcher;
|
||||||
|
|
||||||
public ProxyService(LogChannelService logChannel, IDataStore data, ILogger logger,
|
public ProxyService(LogChannelService logChannel, IDataStore data, ILogger logger,
|
||||||
WebhookExecutorService webhookExecutor, Database db, ProxyMatcher matcher)
|
WebhookExecutorService webhookExecutor, IDatabase db, ProxyMatcher matcher)
|
||||||
{
|
{
|
||||||
_logChannel = logChannel;
|
_logChannel = logChannel;
|
||||||
_data = data;
|
_data = data;
|
||||||
|
@ -16,10 +16,10 @@ namespace PluralKit.Bot {
|
|||||||
public class EmbedService
|
public class EmbedService
|
||||||
{
|
{
|
||||||
private IDataStore _data;
|
private IDataStore _data;
|
||||||
private Database _db;
|
private IDatabase _db;
|
||||||
private DiscordShardedClient _client;
|
private DiscordShardedClient _client;
|
||||||
|
|
||||||
public EmbedService(DiscordShardedClient client, IDataStore data, Database db)
|
public EmbedService(DiscordShardedClient client, IDataStore data, IDatabase db)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_data = data;
|
_data = data;
|
||||||
|
@ -14,12 +14,12 @@ using Serilog;
|
|||||||
namespace PluralKit.Bot {
|
namespace PluralKit.Bot {
|
||||||
public class LogChannelService {
|
public class LogChannelService {
|
||||||
private readonly EmbedService _embed;
|
private readonly EmbedService _embed;
|
||||||
private readonly Database _db;
|
private readonly IDatabase _db;
|
||||||
private readonly IDataStore _data;
|
private readonly IDataStore _data;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly DiscordRestClient _rest;
|
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;
|
_embed = embed;
|
||||||
_rest = rest;
|
_rest = rest;
|
||||||
|
@ -53,10 +53,10 @@ namespace PluralKit.Bot
|
|||||||
.Where(b => b.WebhookName != null)
|
.Where(b => b.WebhookName != null)
|
||||||
.ToDictionary(b => b.WebhookName);
|
.ToDictionary(b => b.WebhookName);
|
||||||
|
|
||||||
private Database _db;
|
private IDatabase _db;
|
||||||
private DiscordShardedClient _client;
|
private DiscordShardedClient _client;
|
||||||
|
|
||||||
public LoggerCleanService(Database db, DiscordShardedClient client)
|
public LoggerCleanService(IDatabase db, DiscordShardedClient client)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
_client = client;
|
_client = client;
|
||||||
|
@ -23,7 +23,7 @@ namespace PluralKit.Bot
|
|||||||
private IMetrics _metrics;
|
private IMetrics _metrics;
|
||||||
private CpuStatService _cpu;
|
private CpuStatService _cpu;
|
||||||
|
|
||||||
private Database _db;
|
private IDatabase _db;
|
||||||
|
|
||||||
private WebhookCacheService _webhookCache;
|
private WebhookCacheService _webhookCache;
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
private ILogger _logger;
|
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;
|
_client = client;
|
||||||
_metrics = metrics;
|
_metrics = metrics;
|
||||||
|
@ -14,7 +14,7 @@ using Serilog;
|
|||||||
|
|
||||||
namespace PluralKit.Core
|
namespace PluralKit.Core
|
||||||
{
|
{
|
||||||
public class Database
|
internal class Database: IDatabase
|
||||||
{
|
{
|
||||||
private readonly CoreConfig _config;
|
private readonly CoreConfig _config;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
@ -65,18 +65,6 @@ namespace PluralKit.Core
|
|||||||
return conn;
|
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>
|
private class PassthroughTypeHandler<T>: SqlMapper.TypeHandler<T>
|
||||||
{
|
{
|
||||||
public override void SetValue(IDbDataParameter parameter, T value) => parameter.Value = value;
|
public override void SetValue(IDbDataParameter parameter, T value) => parameter.Value = value;
|
||||||
|
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 string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files
|
||||||
private const int TargetSchemaVersion = 7;
|
private const int TargetSchemaVersion = 7;
|
||||||
|
|
||||||
private Database _conn;
|
private IDatabase _conn;
|
||||||
private ILogger _logger;
|
private ILogger _logger;
|
||||||
|
|
||||||
public Schemas(Database conn, ILogger logger)
|
public Schemas(IDatabase conn, ILogger logger)
|
||||||
{
|
{
|
||||||
_conn = conn;
|
_conn = conn;
|
||||||
_logger = logger.ForContext<Schemas>();
|
_logger = logger.ForContext<Schemas>();
|
||||||
|
@ -15,9 +15,9 @@ using Serilog;
|
|||||||
|
|
||||||
namespace PluralKit.Core
|
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 PKConnection _ourConnection;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
@ -15,7 +15,7 @@ using Serilog;
|
|||||||
|
|
||||||
namespace PluralKit.Core
|
namespace PluralKit.Core
|
||||||
{
|
{
|
||||||
public class PKConnection: DbConnection, IPKConnection
|
internal class PKConnection: DbConnection, IPKConnection
|
||||||
{
|
{
|
||||||
public NpgsqlConnection Inner { get; }
|
public NpgsqlConnection Inner { get; }
|
||||||
public Guid ConnectionId { get; }
|
public Guid ConnectionId { get; }
|
||||||
|
@ -8,7 +8,7 @@ using Npgsql;
|
|||||||
|
|
||||||
namespace PluralKit.Core
|
namespace PluralKit.Core
|
||||||
{
|
{
|
||||||
public class PKTransaction: DbTransaction, IPKTransaction
|
internal class PKTransaction: DbTransaction, IPKTransaction
|
||||||
{
|
{
|
||||||
public NpgsqlTransaction Inner { get; }
|
public NpgsqlTransaction Inner { get; }
|
||||||
|
|
||||||
|
@ -15,10 +15,10 @@ namespace PluralKit.Core
|
|||||||
public class DataFileService
|
public class DataFileService
|
||||||
{
|
{
|
||||||
private IDataStore _data;
|
private IDataStore _data;
|
||||||
private Database _db;
|
private IDatabase _db;
|
||||||
private ILogger _logger;
|
private ILogger _logger;
|
||||||
|
|
||||||
public DataFileService(ILogger logger, IDataStore data, Database db)
|
public DataFileService(ILogger logger, IDataStore data, IDatabase db)
|
||||||
{
|
{
|
||||||
_data = data;
|
_data = data;
|
||||||
_db = db;
|
_db = db;
|
||||||
|
@ -10,10 +10,10 @@ using Serilog;
|
|||||||
|
|
||||||
namespace PluralKit.Core {
|
namespace PluralKit.Core {
|
||||||
public class PostgresDataStore: IDataStore {
|
public class PostgresDataStore: IDataStore {
|
||||||
private Database _conn;
|
private IDatabase _conn;
|
||||||
private ILogger _logger;
|
private ILogger _logger;
|
||||||
|
|
||||||
public PostgresDataStore(Database conn, ILogger logger)
|
public PostgresDataStore(IDatabase conn, ILogger logger)
|
||||||
{
|
{
|
||||||
_conn = conn;
|
_conn = conn;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
@ -7,7 +7,7 @@ using Dapper;
|
|||||||
namespace PluralKit.Core {
|
namespace PluralKit.Core {
|
||||||
public static class ConnectionUtils
|
public static class ConnectionUtils
|
||||||
{
|
{
|
||||||
public static async IAsyncEnumerable<T> QueryStreamAsync<T>(this Database connFactory, string sql, object param)
|
public static async IAsyncEnumerable<T> QueryStreamAsync<T>(this IDatabase connFactory, string sql, object param)
|
||||||
{
|
{
|
||||||
await using var conn = await connFactory.Obtain();
|
await using var conn = await connFactory.Obtain();
|
||||||
|
|
||||||
|
@ -10,6 +10,11 @@ using NodaTime.Serialization.JsonNet;
|
|||||||
namespace PluralKit.Core {
|
namespace PluralKit.Core {
|
||||||
public static class InitUtils
|
public static class InitUtils
|
||||||
{
|
{
|
||||||
|
public static void InitStatic()
|
||||||
|
{
|
||||||
|
Database.InitStatic();
|
||||||
|
}
|
||||||
|
|
||||||
public static IConfigurationBuilder BuildConfiguration(string[] args) => new ConfigurationBuilder()
|
public static IConfigurationBuilder BuildConfiguration(string[] args) => new ConfigurationBuilder()
|
||||||
.SetBasePath(Directory.GetCurrentDirectory())
|
.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
.AddJsonFile("pluralkit.conf", true)
|
.AddJsonFile("pluralkit.conf", true)
|
||||||
|
Loading…
Reference in New Issue
Block a user