run dotnet format
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
@@ -18,7 +18,7 @@ namespace PluralKit.Core
|
||||
{
|
||||
internal class Database: IDatabase
|
||||
{
|
||||
|
||||
|
||||
private readonly CoreConfig _config;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IMetrics _metrics;
|
||||
@@ -34,20 +34,22 @@ namespace PluralKit.Core
|
||||
_metrics = metrics;
|
||||
_migrator = migrator;
|
||||
_logger = logger.ForContext<Database>();
|
||||
|
||||
|
||||
_connectionString = new NpgsqlConnectionStringBuilder(_config.Database)
|
||||
{
|
||||
Pooling = true, Enlist = false, NoResetOnClose = true,
|
||||
|
||||
Pooling = true,
|
||||
Enlist = false,
|
||||
NoResetOnClose = true,
|
||||
|
||||
// Lower timeout than default (15s -> 2s), should ideally fail-fast instead of hanging
|
||||
Timeout = 2
|
||||
}.ConnectionString;
|
||||
}
|
||||
|
||||
|
||||
public static void InitStatic()
|
||||
{
|
||||
DefaultTypeMap.MatchNamesWithUnderscores = true;
|
||||
|
||||
|
||||
// Dapper by default tries to pass ulongs to Npgsql, which rejects them since PostgreSQL technically
|
||||
// doesn't support unsigned types on its own.
|
||||
// Instead we add a custom mapper to encode them as signed integers instead, converting them back and forth.
|
||||
@@ -61,7 +63,7 @@ namespace PluralKit.Core
|
||||
// So we add a custom type handler that literally just passes the type through to Npgsql
|
||||
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<Instant>());
|
||||
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<LocalDate>());
|
||||
|
||||
|
||||
// Add ID types to Dapper
|
||||
SqlMapper.AddTypeHandler(new NumericIdHandler<SystemId, int>(i => new SystemId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdHandler<MemberId, int>(i => new MemberId(i)));
|
||||
@@ -71,25 +73,25 @@ namespace PluralKit.Core
|
||||
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<MemberId, int>(i => new MemberId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<SwitchId, int>(i => new SwitchId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<GroupId, int>(i => new GroupId(i)));
|
||||
|
||||
|
||||
// Register our custom types to Npgsql
|
||||
// Without these it'll still *work* but break at the first launch + probably cause other small issues
|
||||
NpgsqlConnection.GlobalTypeMapper.MapComposite<ProxyTag>("proxy_tag");
|
||||
NpgsqlConnection.GlobalTypeMapper.MapEnum<PrivacyLevel>("privacy_level");
|
||||
}
|
||||
|
||||
|
||||
public async Task<IPKConnection> Obtain()
|
||||
{
|
||||
// Mark the request (for a handle, I guess) in the metrics
|
||||
_metrics.Measure.Meter.Mark(CoreMetrics.DatabaseRequests);
|
||||
|
||||
|
||||
// Create a connection and open it
|
||||
// We wrap it in PKConnection for tracing purposes
|
||||
var conn = new PKConnection(new NpgsqlConnection(_connectionString), _countHolder, _logger, _metrics);
|
||||
await conn.OpenAsync();
|
||||
return conn;
|
||||
}
|
||||
|
||||
|
||||
public async Task ApplyMigrations()
|
||||
{
|
||||
using var conn = await Obtain();
|
||||
@@ -99,28 +101,28 @@ namespace PluralKit.Core
|
||||
private class PassthroughTypeHandler<T>: SqlMapper.TypeHandler<T>
|
||||
{
|
||||
public override void SetValue(IDbDataParameter parameter, T value) => parameter.Value = value;
|
||||
public override T Parse(object value) => (T) value;
|
||||
public override T Parse(object value) => (T)value;
|
||||
}
|
||||
|
||||
private class UlongEncodeAsLongHandler: SqlMapper.TypeHandler<ulong>
|
||||
{
|
||||
public override ulong Parse(object value) =>
|
||||
// Cast to long to unbox, then to ulong (???)
|
||||
(ulong) (long) value;
|
||||
(ulong)(long)value;
|
||||
|
||||
public override void SetValue(IDbDataParameter parameter, ulong value) => parameter.Value = (long) value;
|
||||
public override void SetValue(IDbDataParameter parameter, ulong value) => parameter.Value = (long)value;
|
||||
}
|
||||
|
||||
private class UlongArrayHandler: SqlMapper.TypeHandler<ulong[]>
|
||||
{
|
||||
public override void SetValue(IDbDataParameter parameter, ulong[] value) => parameter.Value = Array.ConvertAll(value, i => (long) i);
|
||||
public override void SetValue(IDbDataParameter parameter, ulong[] value) => parameter.Value = Array.ConvertAll(value, i => (long)i);
|
||||
|
||||
public override ulong[] Parse(object value) => Array.ConvertAll((long[]) value, i => (ulong) i);
|
||||
public override ulong[] Parse(object value) => Array.ConvertAll((long[])value, i => (ulong)i);
|
||||
}
|
||||
|
||||
private class NumericIdHandler<T, TInner>: SqlMapper.TypeHandler<T>
|
||||
where T: INumericId<T, TInner>
|
||||
where TInner: IEquatable<TInner>, IComparable<TInner>
|
||||
where T : INumericId<T, TInner>
|
||||
where TInner : IEquatable<TInner>, IComparable<TInner>
|
||||
{
|
||||
private readonly Func<TInner, T> _factory;
|
||||
|
||||
@@ -131,12 +133,12 @@ namespace PluralKit.Core
|
||||
|
||||
public override void SetValue(IDbDataParameter parameter, T value) => parameter.Value = value.Value;
|
||||
|
||||
public override T Parse(object value) => _factory((TInner) value);
|
||||
public override T Parse(object value) => _factory((TInner)value);
|
||||
}
|
||||
|
||||
private class NumericIdArrayHandler<T, TInner>: SqlMapper.TypeHandler<T[]>
|
||||
where T: INumericId<T, TInner>
|
||||
where TInner: IEquatable<TInner>, IComparable<TInner>
|
||||
where T : INumericId<T, TInner>
|
||||
where TInner : IEquatable<TInner>, IComparable<TInner>
|
||||
{
|
||||
private readonly Func<TInner, T> _factory;
|
||||
|
||||
@@ -147,10 +149,10 @@ namespace PluralKit.Core
|
||||
|
||||
public override void SetValue(IDbDataParameter parameter, T[] value) => parameter.Value = Array.ConvertAll(value, v => v.Value);
|
||||
|
||||
public override T[] Parse(object value) => Array.ConvertAll((TInner[]) value, v => _factory(v));
|
||||
public override T[] Parse(object value) => Array.ConvertAll((TInner[])value, v => _factory(v));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class DatabaseExt
|
||||
{
|
||||
public static async Task Execute(this IDatabase db, Func<IPKConnection, Task> func)
|
||||
@@ -164,11 +166,11 @@ namespace PluralKit.Core
|
||||
await using var conn = await db.Obtain();
|
||||
return await func(conn);
|
||||
}
|
||||
|
||||
|
||||
public static async IAsyncEnumerable<T> Execute<T>(this IDatabase db, Func<IPKConnection, IAsyncEnumerable<T>> func)
|
||||
{
|
||||
await using var conn = await db.Obtain();
|
||||
|
||||
|
||||
await foreach (var val in func(conn))
|
||||
yield return val;
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
|
||||
using NodaTime;
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace PluralKit.Core
|
||||
@@ -11,11 +11,11 @@ namespace PluralKit.Core
|
||||
public MemberId Id { get; }
|
||||
public IReadOnlyCollection<ProxyTag> ProxyTags { get; } = new ProxyTag[0];
|
||||
public bool KeepProxy { get; }
|
||||
|
||||
|
||||
public string? ServerName { get; }
|
||||
public string? DisplayName { get; }
|
||||
public string Name { get; } = "";
|
||||
|
||||
|
||||
public string? ServerAvatar { get; }
|
||||
public string? Avatar { get; }
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
|
@@ -5,23 +5,23 @@ using Dapper;
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public Task SaveCommandMessage(IPKConnection conn, ulong messageId, ulong authorId) =>
|
||||
conn.QueryAsync("insert into command_messages (message_id, author_id) values (@Message, @Author)",
|
||||
new {Message = messageId, Author = authorId });
|
||||
{
|
||||
public Task SaveCommandMessage(IPKConnection conn, ulong messageId, ulong authorId) =>
|
||||
conn.QueryAsync("insert into command_messages (message_id, author_id) values (@Message, @Author)",
|
||||
new { Message = messageId, Author = authorId });
|
||||
|
||||
public Task<CommandMessage> GetCommandMessage(IPKConnection conn, ulong messageId) =>
|
||||
conn.QuerySingleOrDefaultAsync<CommandMessage>("select * from command_messages where message_id = @Message",
|
||||
new {Message = messageId});
|
||||
public Task<CommandMessage> GetCommandMessage(IPKConnection conn, ulong messageId) =>
|
||||
conn.QuerySingleOrDefaultAsync<CommandMessage>("select * from command_messages where message_id = @Message",
|
||||
new { Message = messageId });
|
||||
|
||||
public Task<int> DeleteCommandMessagesBefore(IPKConnection conn, ulong messageIdThreshold) =>
|
||||
conn.ExecuteAsync("delete from command_messages where message_id < @Threshold",
|
||||
new {Threshold = messageIdThreshold});
|
||||
new { Threshold = messageIdThreshold });
|
||||
}
|
||||
|
||||
public class CommandMessage
|
||||
{
|
||||
public class CommandMessage
|
||||
{
|
||||
public ulong AuthorId { get; set; }
|
||||
public ulong MessageId { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -10,16 +10,16 @@ namespace PluralKit.Core
|
||||
{
|
||||
public Task<MessageContext> GetMessageContext(IPKConnection conn, ulong account, ulong guild, ulong channel)
|
||||
{
|
||||
return conn.QueryFirstAsync<MessageContext>("message_context",
|
||||
new { account_id = account, guild_id = guild, channel_id = channel },
|
||||
return conn.QueryFirstAsync<MessageContext>("message_context",
|
||||
new { account_id = account, guild_id = guild, channel_id = channel },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Task<IEnumerable<ProxyMember>> GetProxyMembers(IPKConnection conn, ulong account, ulong guild)
|
||||
{
|
||||
return conn.QueryAsync<ProxyMember>("proxy_members",
|
||||
new { account_id = account, guild_id = guild },
|
||||
return conn.QueryAsync<ProxyMember>("proxy_members",
|
||||
new { account_id = account, guild_id = guild },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
@@ -12,14 +12,14 @@ namespace PluralKit.Core
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public Task<PKGroup?> GetGroupByName(IPKConnection conn, SystemId system, string name) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(Name) = lower(@Name)", new {System = system, Name = name});
|
||||
|
||||
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(Name) = lower(@Name)", new { System = system, Name = name });
|
||||
|
||||
public Task<PKGroup?> GetGroupByDisplayName(IPKConnection conn, SystemId system, string display_name) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(display_name) = lower(@Name)", new {System = system, Name = display_name});
|
||||
|
||||
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(display_name) = lower(@Name)", new { System = system, Name = display_name });
|
||||
|
||||
public Task<PKGroup?> GetGroupByHid(IPKConnection conn, string hid) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where hid = @hid", new {hid = hid.ToLowerInvariant()});
|
||||
|
||||
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where hid = @hid", new { hid = hid.ToLowerInvariant() });
|
||||
|
||||
public Task<int> GetGroupMemberCount(IPKConnection conn, GroupId id, PrivacyLevel? privacyFilter = null)
|
||||
{
|
||||
var query = new StringBuilder("select count(*) from group_members");
|
||||
@@ -28,14 +28,14 @@ namespace PluralKit.Core
|
||||
query.Append(" where group_members.group_id = @Id");
|
||||
if (privacyFilter != null)
|
||||
query.Append(" and members.member_visibility = @PrivacyFilter");
|
||||
return conn.QuerySingleOrDefaultAsync<int>(query.ToString(), new {Id = id, PrivacyFilter = privacyFilter});
|
||||
return conn.QuerySingleOrDefaultAsync<int>(query.ToString(), new { Id = id, PrivacyFilter = privacyFilter });
|
||||
}
|
||||
|
||||
|
||||
public async Task<PKGroup> CreateGroup(IPKConnection conn, SystemId system, string name, IDbTransaction? transaction = null)
|
||||
{
|
||||
var group = await conn.QueryFirstAsync<PKGroup>(
|
||||
"insert into groups (hid, system, name) values (find_free_group_hid(), @System, @Name) returning *",
|
||||
new {System = system, Name = name}, transaction);
|
||||
new { System = system, Name = name }, transaction);
|
||||
_logger.Information("Created group {GroupId} in system {SystemId}: {GroupName}", group.Id, system, name);
|
||||
return group;
|
||||
}
|
||||
@@ -52,7 +52,7 @@ namespace PluralKit.Core
|
||||
public Task DeleteGroup(IPKConnection conn, GroupId group)
|
||||
{
|
||||
_logger.Information("Deleted {GroupId}", group);
|
||||
return conn.ExecuteAsync("delete from groups where id = @Id", new {Id = @group});
|
||||
return conn.ExecuteAsync("delete from groups where id = @Id", new { Id = @group });
|
||||
}
|
||||
|
||||
public async Task AddMembersToGroup(IPKConnection conn, GroupId group,
|
||||
@@ -76,7 +76,7 @@ namespace PluralKit.Core
|
||||
{
|
||||
_logger.Information("Removed members from {GroupId}: {MemberIds}", group, members);
|
||||
return conn.ExecuteAsync("delete from group_members where group_id = @Group and member_id = any(@Members)",
|
||||
new {Group = @group, Members = members.ToArray()});
|
||||
new { Group = @group, Members = members.ToArray() });
|
||||
}
|
||||
}
|
||||
}
|
@@ -7,12 +7,12 @@ using Dapper;
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public partial class ModelRepository
|
||||
{
|
||||
{
|
||||
public IAsyncEnumerable<PKGroup> GetMemberGroups(IPKConnection conn, MemberId id) =>
|
||||
conn.QueryStreamAsync<PKGroup>(
|
||||
"select groups.* from group_members inner join groups on group_members.group_id = groups.id where group_members.member_id = @Id",
|
||||
new {Id = id});
|
||||
|
||||
new { Id = id });
|
||||
|
||||
|
||||
public async Task AddGroupsToMember(IPKConnection conn, MemberId member, IReadOnlyCollection<GroupId> groups)
|
||||
{
|
||||
@@ -33,7 +33,7 @@ namespace PluralKit.Core
|
||||
{
|
||||
_logger.Information("Removed groups from {MemberId}: {GroupIds}", member, groups);
|
||||
return conn.ExecuteAsync("delete from group_members where member_id = @Member and group_id = any(@Groups)",
|
||||
new {Member = @member, Groups = groups.ToArray() });
|
||||
new { Member = @member, Groups = groups.ToArray() });
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace PluralKit.Core
|
||||
.Build();
|
||||
return conn.ExecuteAsync(query, pms);
|
||||
}
|
||||
|
||||
|
||||
public Task UpsertSystemGuild(IPKConnection conn, SystemId system, ulong guild,
|
||||
SystemGuildPatch patch)
|
||||
{
|
||||
@@ -36,18 +36,18 @@ namespace PluralKit.Core
|
||||
.Build();
|
||||
return conn.ExecuteAsync(query, pms);
|
||||
}
|
||||
|
||||
|
||||
public Task<GuildConfig> GetGuild(IPKConnection conn, ulong guild) =>
|
||||
conn.QueryFirstAsync<GuildConfig>("insert into servers (id) values (@guild) on conflict (id) do update set id = @guild returning *", new {guild});
|
||||
conn.QueryFirstAsync<GuildConfig>("insert into servers (id) values (@guild) on conflict (id) do update set id = @guild returning *", new { guild });
|
||||
|
||||
public Task<SystemGuildSettings> GetSystemGuild(IPKConnection conn, ulong guild, SystemId system) =>
|
||||
conn.QueryFirstAsync<SystemGuildSettings>(
|
||||
"insert into system_guild (guild, system) values (@guild, @system) on conflict (guild, system) do update set guild = @guild, system = @system returning *",
|
||||
new {guild, system});
|
||||
"insert into system_guild (guild, system) values (@guild, @system) on conflict (guild, system) do update set guild = @guild, system = @system returning *",
|
||||
new { guild, system });
|
||||
|
||||
public Task<MemberGuildSettings> GetMemberGuild(IPKConnection conn, ulong guild, MemberId member) =>
|
||||
conn.QueryFirstAsync<MemberGuildSettings>(
|
||||
"insert into member_guild (guild, member) values (@guild, @member) on conflict (guild, member) do update set guild = @guild, member = @member returning *",
|
||||
new {guild, member});
|
||||
new { guild, member });
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using System.Data;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -9,22 +9,22 @@ namespace PluralKit.Core
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public Task<PKMember?> GetMember(IPKConnection conn, MemberId id) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where id = @id", new {id});
|
||||
|
||||
public Task<PKMember?> GetMemberByHid(IPKConnection conn, string hid) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where id = @id", new { id });
|
||||
|
||||
public Task<PKMember?> GetMemberByHid(IPKConnection conn, string hid) =>
|
||||
conn.QuerySingleOrDefaultAsync<PKMember?>("select * from members where hid = @Hid", new { Hid = hid.ToLower() });
|
||||
|
||||
public Task<PKMember?> GetMemberByName(IPKConnection conn, SystemId system, string name) =>
|
||||
public Task<PKMember?> GetMemberByName(IPKConnection conn, SystemId system, string name) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });
|
||||
|
||||
public Task<PKMember?> GetMemberByDisplayName(IPKConnection conn, SystemId system, string name) =>
|
||||
public Task<PKMember?> GetMemberByDisplayName(IPKConnection conn, SystemId system, string name) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(display_name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });
|
||||
|
||||
public async Task<PKMember> CreateMember(IPKConnection conn, SystemId id, string memberName, IDbTransaction? transaction = null)
|
||||
{
|
||||
var member = await conn.QueryFirstAsync<PKMember>(
|
||||
"insert into members (hid, system, name) values (find_free_member_hid(), @SystemId, @Name) returning *",
|
||||
new {SystemId = id, Name = memberName}, transaction);
|
||||
new { SystemId = id, Name = memberName }, transaction);
|
||||
_logger.Information("Created {MemberId} in {SystemId}: {MemberName}",
|
||||
member.Id, id, memberName);
|
||||
return member;
|
||||
@@ -42,7 +42,7 @@ namespace PluralKit.Core
|
||||
public Task DeleteMember(IPKConnection conn, MemberId id)
|
||||
{
|
||||
_logger.Information("Deleted {MemberId}", id);
|
||||
return conn.ExecuteAsync("delete from members where id = @Id", new {Id = id});
|
||||
return conn.ExecuteAsync("delete from members where id = @Id", new { Id = id });
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -8,26 +8,27 @@ namespace PluralKit.Core
|
||||
{
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public async Task AddMessage(IPKConnection conn, PKMessage msg) {
|
||||
public async Task AddMessage(IPKConnection conn, PKMessage msg)
|
||||
{
|
||||
// "on conflict do nothing" in the (pretty rare) case of duplicate events coming in from Discord, which would lead to a DB error before
|
||||
await conn.ExecuteAsync("insert into messages(mid, guild, channel, member, sender, original_mid) values(@Mid, @Guild, @Channel, @Member, @Sender, @OriginalMid) on conflict do nothing", msg);
|
||||
_logger.Debug("Stored message {@StoredMessage} in channel {Channel}", msg, msg.Channel);
|
||||
}
|
||||
|
||||
|
||||
public async Task<FullMessage?> GetMessage(IPKConnection conn, ulong id)
|
||||
{
|
||||
FullMessage Mapper(PKMessage msg, PKMember member, PKSystem system) =>
|
||||
new FullMessage {Message = msg, System = system, Member = member};
|
||||
new FullMessage { Message = msg, System = system, Member = member };
|
||||
|
||||
var result = await conn.QueryAsync<PKMessage, PKMember, PKSystem, FullMessage>(
|
||||
"select messages.*, members.*, systems.* from messages, members, systems where (mid = @Id or original_mid = @Id) and messages.member = members.id and systems.id = members.system",
|
||||
Mapper, new {Id = id});
|
||||
Mapper, new { Id = id });
|
||||
return result.FirstOrDefault();
|
||||
}
|
||||
|
||||
public async Task DeleteMessage(IPKConnection conn, ulong id)
|
||||
{
|
||||
var rowCount = await conn.ExecuteAsync("delete from messages where mid = @Id", new {Id = id});
|
||||
var rowCount = await conn.ExecuteAsync("delete from messages where mid = @Id", new { Id = id });
|
||||
if (rowCount > 0)
|
||||
_logger.Information("Deleted message {MessageId} from database", id);
|
||||
}
|
||||
@@ -37,7 +38,7 @@ namespace PluralKit.Core
|
||||
// Npgsql doesn't support ulongs in general - we hacked around it for plain ulongs but tbh not worth it for collections of ulong
|
||||
// Hence we map them to single longs, which *are* supported (this is ok since they're Technically (tm) stored as signed longs in the db anyway)
|
||||
var rowCount = await conn.ExecuteAsync("delete from messages where mid = any(@Ids)",
|
||||
new {Ids = ids.Select(id => (long) id).ToArray()});
|
||||
new { Ids = ids.Select(id => (long)id).ToArray() });
|
||||
if (rowCount > 0)
|
||||
_logger.Information("Bulk deleted messages ({FoundCount} found) from database: {MessageIds}", rowCount,
|
||||
ids);
|
||||
@@ -65,7 +66,7 @@ namespace PluralKit.Core
|
||||
public ulong Sender { get; set; }
|
||||
public ulong? OriginalMid { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class FullMessage
|
||||
{
|
||||
public PKMessage Message;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
@@ -15,16 +15,16 @@ namespace PluralKit.Core
|
||||
public Task SetShardStatus(IPKConnection conn, int shard, PKShardInfo.ShardStatus status) =>
|
||||
conn.ExecuteAsync(
|
||||
"insert into shards (id, status) values (@Id, @Status) on conflict (id) do update set status = @Status",
|
||||
new {Id = shard, Status = status});
|
||||
new { Id = shard, Status = status });
|
||||
|
||||
public Task RegisterShardHeartbeat(IPKConnection conn, int shard, Duration ping) =>
|
||||
conn.ExecuteAsync(
|
||||
"insert into shards (id, last_heartbeat, ping) values (@Id, now(), @Ping) on conflict (id) do update set last_heartbeat = now(), ping = @Ping",
|
||||
new {Id = shard, Ping = ping.TotalSeconds});
|
||||
new { Id = shard, Ping = ping.TotalSeconds });
|
||||
|
||||
public Task RegisterShardConnection(IPKConnection conn, int shard) =>
|
||||
conn.ExecuteAsync(
|
||||
"insert into shards (id, last_connection) values (@Id, now()) on conflict (id) do update set last_connection = now()",
|
||||
new {Id = shard});
|
||||
new { Id = shard });
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace PluralKit.Core
|
||||
|
||||
// First, we insert the switch itself
|
||||
var sw = await conn.QuerySingleAsync<PKSwitch>("insert into switches(system) values (@System) returning *",
|
||||
new {System = system});
|
||||
new { System = system });
|
||||
|
||||
// Then we insert each member in the switch in the switch_members table
|
||||
await using (var w = conn.BeginBinaryImport("copy switch_members (switch, member) from stdin (format binary)"))
|
||||
@@ -43,20 +43,20 @@ namespace PluralKit.Core
|
||||
public async Task MoveSwitch(IPKConnection conn, SwitchId id, Instant time)
|
||||
{
|
||||
await conn.ExecuteAsync("update switches set timestamp = @Time where id = @Id",
|
||||
new {Time = time, Id = id});
|
||||
new { Time = time, Id = id });
|
||||
|
||||
_logger.Information("Updated {SwitchId} timestamp: {SwitchTimestamp}", id, time);
|
||||
}
|
||||
|
||||
public async Task DeleteSwitch(IPKConnection conn, SwitchId id)
|
||||
{
|
||||
await conn.ExecuteAsync("delete from switches where id = @Id", new {Id = id});
|
||||
await conn.ExecuteAsync("delete from switches where id = @Id", new { Id = id });
|
||||
_logger.Information("Deleted {Switch}", id);
|
||||
}
|
||||
|
||||
public async Task DeleteAllSwitches(IPKConnection conn, SystemId system)
|
||||
{
|
||||
await conn.ExecuteAsync("delete from switches where system = @Id", new {Id = system});
|
||||
await conn.ExecuteAsync("delete from switches where system = @Id", new { Id = system });
|
||||
_logger.Information("Deleted all switches in {SystemId}", system);
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ namespace PluralKit.Core
|
||||
// TODO: refactor the PKSwitch data structure to somehow include a hydrated member list
|
||||
return conn.QueryStreamAsync<PKSwitch>(
|
||||
"select * from switches where system = @System order by timestamp desc",
|
||||
new {System = system});
|
||||
new { System = system });
|
||||
}
|
||||
|
||||
public async Task<int> GetSwitchCount(IPKConnection conn, SystemId system)
|
||||
@@ -86,7 +86,7 @@ namespace PluralKit.Core
|
||||
FROM switches
|
||||
WHERE switches.system = @System
|
||||
AND switches.timestamp < @Start",
|
||||
new {System = system, Start = start});
|
||||
new { System = system, Start = start });
|
||||
|
||||
// Then collect the time and members of all switches that overlap the range
|
||||
var switchMembersEntries = conn.QueryStreamAsync<SwitchMembersListEntry>(
|
||||
@@ -101,7 +101,7 @@ namespace PluralKit.Core
|
||||
)
|
||||
AND switches.timestamp < @End
|
||||
ORDER BY switches.timestamp DESC",
|
||||
new {System = system, Start = start, End = end, LastSwitch = lastSwitch});
|
||||
new { System = system, Start = start, End = end, LastSwitch = lastSwitch });
|
||||
|
||||
// Yield each value here
|
||||
await foreach (var entry in switchMembersEntries)
|
||||
@@ -114,7 +114,7 @@ namespace PluralKit.Core
|
||||
{
|
||||
return conn.QueryStreamAsync<PKMember>(
|
||||
"select * from switch_members, members where switch_members.member = members.id and switch_members.switch = @Switch order by switch_members.id",
|
||||
new {Switch = sw});
|
||||
new { Switch = sw });
|
||||
}
|
||||
|
||||
public async Task<PKSwitch> GetLatestSwitch(IPKConnection conn, SystemId system) =>
|
||||
@@ -136,13 +136,13 @@ namespace PluralKit.Core
|
||||
// key used in GetPerMemberSwitchDuration below
|
||||
var membersList = await conn.QueryAsync<PKMember>(
|
||||
"select * from members where id = any(@Switches)", // lol postgres specific `= any()` syntax
|
||||
new {Switches = switchMembers.Select(m => m.Member.Value).Distinct().ToList()});
|
||||
new { Switches = switchMembers.Select(m => m.Member.Value).Distinct().ToList() });
|
||||
var memberObjects = membersList.ToDictionary(m => m.Id);
|
||||
|
||||
// check if a group ID is provided. if so, query DB for all members of said group, otherwise use membersList
|
||||
var groupMembersList = group != null ? await conn.QueryAsync<PKMember>(
|
||||
"select * from members inner join group_members on members.id = group_members.member_id where group_id = @id",
|
||||
new {id = group}) : membersList;
|
||||
new { id = group }) : membersList;
|
||||
var groupMemberObjects = groupMembersList.ToDictionary(m => m.Id);
|
||||
|
||||
// Initialize entries - still need to loop to determine the TimespanEnd below
|
||||
@@ -154,7 +154,7 @@ namespace PluralKit.Core
|
||||
select new SwitchListEntry
|
||||
{
|
||||
TimespanStart = g.Key,
|
||||
Members = g.Where(x => x.Member != default(MemberId) && groupMemberObjects.Any(m => x.Member == m.Key) ).Select(x => memberObjects[x.Member])
|
||||
Members = g.Where(x => x.Member != default(MemberId) && groupMemberObjects.Any(m => x.Member == m.Key)).Select(x => memberObjects[x.Member])
|
||||
.ToList()
|
||||
};
|
||||
|
||||
@@ -171,7 +171,9 @@ namespace PluralKit.Core
|
||||
|
||||
outList.Add(new SwitchListEntry
|
||||
{
|
||||
Members = e.Members, TimespanStart = switchStartClamped, TimespanEnd = endTime
|
||||
Members = e.Members,
|
||||
TimespanStart = switchStartClamped,
|
||||
TimespanEnd = endTime
|
||||
});
|
||||
|
||||
// next switch's end is this switch's start (we're working backward in time)
|
||||
@@ -219,7 +221,7 @@ namespace PluralKit.Core
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public struct SwitchListEntry
|
||||
{
|
||||
public ICollection<PKMember> Members;
|
||||
@@ -234,7 +236,7 @@ namespace PluralKit.Core
|
||||
public Instant RangeStart;
|
||||
public Instant RangeEnd;
|
||||
}
|
||||
|
||||
|
||||
public struct SwitchMembersListEntry
|
||||
{
|
||||
public MemberId Member;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -10,36 +10,36 @@ namespace PluralKit.Core
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public Task<PKSystem?> GetSystem(IPKConnection conn, SystemId id) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKSystem?>("select * from systems where id = @id", new {id});
|
||||
conn.QueryFirstOrDefaultAsync<PKSystem?>("select * from systems where id = @id", new { id });
|
||||
|
||||
public Task<PKSystem?> GetSystemByAccount(IPKConnection conn, ulong accountId) =>
|
||||
conn.QuerySingleOrDefaultAsync<PKSystem?>(
|
||||
"select systems.* from systems, accounts where accounts.system = systems.id and accounts.uid = @Id",
|
||||
new {Id = accountId});
|
||||
new { Id = accountId });
|
||||
|
||||
public Task<PKSystem?> GetSystemByHid(IPKConnection conn, string hid) =>
|
||||
conn.QuerySingleOrDefaultAsync<PKSystem?>("select * from systems where systems.hid = @Hid",
|
||||
new {Hid = hid.ToLower()});
|
||||
new { Hid = hid.ToLower() });
|
||||
|
||||
public Task<IEnumerable<ulong>> GetSystemAccounts(IPKConnection conn, SystemId system) =>
|
||||
conn.QueryAsync<ulong>("select uid from accounts where system = @Id", new {Id = system});
|
||||
conn.QueryAsync<ulong>("select uid from accounts where system = @Id", new { Id = system });
|
||||
|
||||
public IAsyncEnumerable<PKMember> GetSystemMembers(IPKConnection conn, SystemId system) =>
|
||||
conn.QueryStreamAsync<PKMember>("select * from members where system = @SystemID", new {SystemID = system});
|
||||
conn.QueryStreamAsync<PKMember>("select * from members where system = @SystemID", new { SystemID = system });
|
||||
|
||||
public Task<int> GetSystemMemberCount(IPKConnection conn, SystemId id, PrivacyLevel? privacyFilter = null)
|
||||
{
|
||||
var query = new StringBuilder("select count(*) from members where system = @Id");
|
||||
if (privacyFilter != null)
|
||||
query.Append($" and member_visibility = {(int) privacyFilter.Value}");
|
||||
return conn.QuerySingleAsync<int>(query.ToString(), new {Id = id});
|
||||
query.Append($" and member_visibility = {(int)privacyFilter.Value}");
|
||||
return conn.QuerySingleAsync<int>(query.ToString(), new { Id = id });
|
||||
}
|
||||
|
||||
public async Task<PKSystem> CreateSystem(IPKConnection conn, string? systemName = null, IPKTransaction? tx = null)
|
||||
{
|
||||
var system = await conn.QuerySingleAsync<PKSystem>(
|
||||
"insert into systems (hid, name) values (find_free_system_hid(), @Name) returning *",
|
||||
new {Name = systemName},
|
||||
new { Name = systemName },
|
||||
transaction: tx);
|
||||
_logger.Information("Created {SystemId}", system.Id);
|
||||
return system;
|
||||
@@ -59,21 +59,21 @@ namespace PluralKit.Core
|
||||
// We have "on conflict do nothing" since linking an account when it's already linked to the same system is idempotent
|
||||
// This is used in import/export, although the pk;link command checks for this case beforehand
|
||||
await conn.ExecuteAsync("insert into accounts (uid, system) values (@Id, @SystemId) on conflict do nothing",
|
||||
new {Id = accountId, SystemId = system});
|
||||
new { Id = accountId, SystemId = system });
|
||||
_logger.Information("Linked account {UserId} to {SystemId}", accountId, system);
|
||||
}
|
||||
|
||||
public async Task RemoveAccount(IPKConnection conn, SystemId system, ulong accountId)
|
||||
{
|
||||
await conn.ExecuteAsync("delete from accounts where uid = @Id and system = @SystemId",
|
||||
new {Id = accountId, SystemId = system});
|
||||
new { Id = accountId, SystemId = system });
|
||||
_logger.Information("Unlinked account {UserId} from {SystemId}", accountId, system);
|
||||
}
|
||||
|
||||
public Task DeleteSystem(IPKConnection conn, SystemId id)
|
||||
{
|
||||
_logger.Information("Deleted {SystemId}", id);
|
||||
return conn.ExecuteAsync("delete from systems where id = @Id", new {Id = id});
|
||||
return conn.ExecuteAsync("delete from systems where id = @Id", new { Id = id });
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
using Serilog;
|
||||
using Serilog;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
|
@@ -1,18 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
|
||||
using Dapper;
|
||||
|
||||
namespace PluralKit.Core {
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public static class ConnectionUtils
|
||||
{
|
||||
public static async IAsyncEnumerable<T> QueryStreamAsync<T>(this IPKConnection conn, string sql, object param)
|
||||
{
|
||||
await using var reader = (DbDataReader) await conn.ExecuteReaderAsync(sql, param);
|
||||
await using var reader = (DbDataReader)await conn.ExecuteReaderAsync(sql, param);
|
||||
var parser = reader.GetRowParser<T>();
|
||||
|
||||
|
||||
while (await reader.ReadAsync())
|
||||
yield return parser(reader);
|
||||
yield return parser(reader);
|
||||
}
|
||||
}
|
||||
}
|
@@ -62,7 +62,7 @@ namespace PluralKit.Core
|
||||
|
||||
// If the above creates new enum/composite types, we must tell Npgsql to reload the internal type caches
|
||||
// This will propagate to every other connection as well, since it marks the global type mapper collection dirty.
|
||||
((PKConnection) conn).ReloadTypes();
|
||||
((PKConnection)conn).ReloadTypes();
|
||||
}
|
||||
|
||||
private async Task<int> GetCurrentDatabaseVersion(IPKConnection conn)
|
||||
@@ -82,4 +82,4 @@ namespace PluralKit.Core
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
using System.Threading;
|
||||
using System.Threading;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
@@ -23,32 +23,32 @@ namespace PluralKit.Core
|
||||
_conflictField = conflictField;
|
||||
_condition = condition;
|
||||
}
|
||||
|
||||
public static QueryBuilder Insert(string table) => new QueryBuilder(QueryType.Insert, table, null, null);
|
||||
|
||||
public static QueryBuilder Insert(string table) => new QueryBuilder(QueryType.Insert, table, null, null);
|
||||
public static QueryBuilder Update(string table, string condition) => new QueryBuilder(QueryType.Update, table, null, condition);
|
||||
public static QueryBuilder Upsert(string table, string conflictField) => new QueryBuilder(QueryType.Upsert, table, conflictField, null);
|
||||
|
||||
public QueryBuilder Constant(string fieldName, string paramName)
|
||||
{
|
||||
if (_firstInsert) _firstInsert = false;
|
||||
else
|
||||
else
|
||||
{
|
||||
_insertFragment.Append(", ");
|
||||
_valuesFragment.Append(", ");
|
||||
}
|
||||
|
||||
|
||||
_insertFragment.Append(fieldName);
|
||||
_valuesFragment.Append(paramName);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryBuilder Variable(string fieldName, string paramName)
|
||||
{
|
||||
Constant(fieldName, paramName);
|
||||
|
||||
|
||||
if (_firstUpdate) _firstUpdate = false;
|
||||
else _updateFragment.Append(", ");
|
||||
|
||||
|
||||
_updateFragment.Append(fieldName);
|
||||
_updateFragment.Append(" = ");
|
||||
_updateFragment.Append(paramName);
|
||||
@@ -59,7 +59,7 @@ namespace PluralKit.Core
|
||||
{
|
||||
if (_firstInsert)
|
||||
throw new ArgumentException("No fields have been added to the query.");
|
||||
|
||||
|
||||
StringBuilder query = new StringBuilder(Type switch
|
||||
{
|
||||
QueryType.Insert => $"insert into {Table} ({_insertFragment}) values ({_valuesFragment})",
|
||||
@@ -70,7 +70,7 @@ namespace PluralKit.Core
|
||||
|
||||
if (Type == QueryType.Update && _condition != null)
|
||||
query.Append($" where {_condition}");
|
||||
|
||||
|
||||
if (!string.IsNullOrEmpty(suffix))
|
||||
query.Append($" {suffix}");
|
||||
query.Append(";");
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System.Text;
|
||||
using System.Text;
|
||||
|
||||
using Dapper;
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace PluralKit.Core
|
||||
{
|
||||
_qb = qb;
|
||||
}
|
||||
|
||||
|
||||
public static UpdateQueryBuilder Insert(string table) => new UpdateQueryBuilder(QueryBuilder.Insert(table));
|
||||
public static UpdateQueryBuilder Update(string table, string condition) => new UpdateQueryBuilder(QueryBuilder.Update(table, condition));
|
||||
public static UpdateQueryBuilder Upsert(string table, string conflictField) => new UpdateQueryBuilder(QueryBuilder.Upsert(table, conflictField));
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -10,11 +10,11 @@ namespace PluralKit.Core
|
||||
public static class DatabaseViewsExt
|
||||
{
|
||||
public static Task<IEnumerable<SystemFronter>> QueryCurrentFronters(this IPKConnection conn, SystemId system) =>
|
||||
conn.QueryAsync<SystemFronter>("select * from system_fronters where system = @system", new {system});
|
||||
conn.QueryAsync<SystemFronter>("select * from system_fronters where system = @system", new { system });
|
||||
|
||||
public static Task<IEnumerable<ListedGroup>> QueryGroupList(this IPKConnection conn, SystemId system) =>
|
||||
conn.QueryAsync<ListedGroup>("select * from group_list where system = @System", new {System = system});
|
||||
|
||||
conn.QueryAsync<ListedGroup>("select * from group_list where system = @System", new { System = system });
|
||||
|
||||
public static Task<IEnumerable<ListedMember>> QueryMemberList(this IPKConnection conn, SystemId system, MemberListQueryOptions opts)
|
||||
{
|
||||
StringBuilder query;
|
||||
@@ -24,11 +24,11 @@ namespace PluralKit.Core
|
||||
query = new StringBuilder("select member_list.* from group_members inner join member_list on member_list.id = group_members.member_id where group_id = @groupFilter");
|
||||
|
||||
if (opts.PrivacyFilter != null)
|
||||
query.Append($" and member_visibility = {(int) opts.PrivacyFilter}");
|
||||
query.Append($" and member_visibility = {(int)opts.PrivacyFilter}");
|
||||
|
||||
if (opts.Search != null)
|
||||
{
|
||||
static string Filter(string column) => $"(position(lower(@filter) in lower(coalesce({column}, ''))) > 0)";
|
||||
static string Filter(string column) => $"(position(lower(@filter) in lower(coalesce({column}, ''))) > 0)";
|
||||
|
||||
query.Append($" and ({Filter("name")} or {Filter("display_name")}");
|
||||
if (opts.SearchDescription)
|
||||
@@ -41,10 +41,10 @@ namespace PluralKit.Core
|
||||
}
|
||||
query.Append(")");
|
||||
}
|
||||
|
||||
return conn.QueryAsync<ListedMember>(query.ToString(), new {system, filter = opts.Search, groupFilter = opts.GroupFilter});
|
||||
|
||||
return conn.QueryAsync<ListedMember>(query.ToString(), new { system, filter = opts.Search, groupFilter = opts.GroupFilter });
|
||||
}
|
||||
|
||||
|
||||
public struct MemberListQueryOptions
|
||||
{
|
||||
public PrivacyLevel? PrivacyFilter;
|
||||
|
@@ -1,6 +1,6 @@
|
||||
namespace PluralKit.Core
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public class ListedGroup : PKGroup
|
||||
public class ListedGroup: PKGroup
|
||||
{
|
||||
public int MemberCount { get; }
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using NodaTime;
|
||||
|
||||
namespace PluralKit.Core
|
||||
@@ -12,6 +12,6 @@ namespace PluralKit.Core
|
||||
public AnnualDate? AnnualBirthday =>
|
||||
Birthday != null
|
||||
? new AnnualDate(Birthday.Value.Month, Birthday.Value.Day)
|
||||
: (AnnualDate?) null;
|
||||
: (AnnualDate?)null;
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
using NodaTime;
|
||||
using NodaTime;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Threading;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -10,18 +10,18 @@ namespace PluralKit.Core
|
||||
public interface IPKConnection: IDbConnection, IAsyncDisposable
|
||||
{
|
||||
public Guid ConnectionId { get; }
|
||||
|
||||
|
||||
public Task OpenAsync(CancellationToken cancellationToken = default);
|
||||
public Task CloseAsync();
|
||||
|
||||
public Task ChangeDatabaseAsync(string databaseName, CancellationToken ct = default);
|
||||
|
||||
|
||||
public ValueTask<IPKTransaction> BeginTransactionAsync(CancellationToken ct = default) => BeginTransactionAsync(IsolationLevel.Unspecified, ct);
|
||||
public ValueTask<IPKTransaction> BeginTransactionAsync(IsolationLevel level, CancellationToken ct = default);
|
||||
|
||||
|
||||
public NpgsqlBinaryImporter BeginBinaryImport(string copyFromCommand);
|
||||
public NpgsqlBinaryExporter BeginBinaryExport(string copyToCommand);
|
||||
|
||||
|
||||
[Obsolete] new void Open();
|
||||
[Obsolete] new void Close();
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
@@ -19,11 +19,11 @@ namespace PluralKit.Core
|
||||
internal class PKCommand: DbCommand, IPKCommand
|
||||
{
|
||||
private NpgsqlCommand Inner { get; }
|
||||
|
||||
|
||||
private readonly PKConnection _ourConnection;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IMetrics _metrics;
|
||||
|
||||
|
||||
public PKCommand(NpgsqlCommand inner, PKConnection ourConnection, ILogger logger, IMetrics metrics)
|
||||
{
|
||||
Inner = inner;
|
||||
@@ -111,16 +111,16 @@ namespace PluralKit.Core
|
||||
{
|
||||
var end = SystemClock.Instance.GetCurrentInstant();
|
||||
var elapsed = end - start;
|
||||
|
||||
|
||||
_logger.Verbose("Executed query {Query} in {ElapsedTime} on connection {ConnectionId}", CommandText, elapsed, _ourConnection.ConnectionId);
|
||||
|
||||
|
||||
// One "BCL compatible tick" is 100 nanoseconds
|
||||
var micros = elapsed.BclCompatibleTicks / 10;
|
||||
_metrics.Provider.Timer.Instance(CoreMetrics.DatabaseQuery, new MetricTags("query", CommandText))
|
||||
.Record(micros, TimeUnit.Microseconds, CommandText);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static Exception SyncError(string caller) => throw new Exception($"Executed synchronous IDbCommand function {caller}!");
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
@@ -37,28 +37,28 @@ namespace PluralKit.Core
|
||||
_logger = logger.ForContext<PKConnection>();
|
||||
_metrics = metrics;
|
||||
}
|
||||
|
||||
|
||||
public override Task OpenAsync(CancellationToken ct)
|
||||
{
|
||||
if (_hasOpened) return Inner.OpenAsync(ct);
|
||||
_countHolder.Increment();
|
||||
_hasOpened = true;
|
||||
_openTime = SystemClock.Instance.GetCurrentInstant();
|
||||
_logger.Verbose("Opened database connection {ConnectionId}, new connection count {ConnectionCount}", ConnectionId, _countHolder.ConnectionCount);
|
||||
_logger.Verbose("Opened database connection {ConnectionId}, new connection count {ConnectionCount}", ConnectionId, _countHolder.ConnectionCount);
|
||||
return Inner.OpenAsync(ct);
|
||||
}
|
||||
|
||||
public override Task CloseAsync() => Inner.CloseAsync();
|
||||
|
||||
protected override DbCommand CreateDbCommand() => new PKCommand(Inner.CreateCommand(), this, _logger, _metrics);
|
||||
|
||||
|
||||
public void ReloadTypes() => Inner.ReloadTypes();
|
||||
|
||||
public new async ValueTask<IPKTransaction> BeginTransactionAsync(IsolationLevel level, CancellationToken ct = default) => new PKTransaction(await Inner.BeginTransactionAsync(level, ct));
|
||||
|
||||
public NpgsqlBinaryImporter BeginBinaryImport(string copyFromCommand) => Inner.BeginBinaryImport(copyFromCommand);
|
||||
public NpgsqlBinaryExporter BeginBinaryExport(string copyToCommand) => Inner.BeginBinaryExport(copyToCommand);
|
||||
|
||||
|
||||
public override void ChangeDatabase(string databaseName) => Inner.ChangeDatabase(databaseName);
|
||||
public override Task ChangeDatabaseAsync(string databaseName, CancellationToken ct = default) => Inner.ChangeDatabaseAsync(databaseName, ct);
|
||||
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) => throw SyncError(nameof(BeginDbTransaction));
|
||||
@@ -85,12 +85,12 @@ namespace PluralKit.Core
|
||||
public override ConnectionState State => Inner.State;
|
||||
public override string DataSource => Inner.DataSource;
|
||||
public override string ServerVersion => Inner.ServerVersion;
|
||||
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
{
|
||||
Inner.Dispose();
|
||||
if (_hasClosed) return;
|
||||
|
||||
|
||||
LogClose();
|
||||
}
|
||||
|
||||
@@ -100,12 +100,12 @@ namespace PluralKit.Core
|
||||
LogClose();
|
||||
return Inner.DisposeAsync();
|
||||
}
|
||||
|
||||
|
||||
private void LogClose()
|
||||
{
|
||||
_countHolder.Decrement();
|
||||
_hasClosed = true;
|
||||
|
||||
|
||||
var duration = SystemClock.Instance.GetCurrentInstant() - _openTime;
|
||||
_logger.Verbose("Closed database connection {ConnectionId} (open for {ConnectionDuration}), new connection count {ConnectionCount}", ConnectionId, duration, _countHolder.ConnectionCount);
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Threading;
|
||||
@@ -11,7 +11,7 @@ namespace PluralKit.Core
|
||||
internal class PKTransaction: DbTransaction, IPKTransaction
|
||||
{
|
||||
public NpgsqlTransaction Inner { get; }
|
||||
|
||||
|
||||
public PKTransaction(NpgsqlTransaction inner)
|
||||
{
|
||||
Inner = inner;
|
||||
@@ -25,7 +25,7 @@ namespace PluralKit.Core
|
||||
|
||||
protected override DbConnection DbConnection => Inner.Connection;
|
||||
public override IsolationLevel IsolationLevel => Inner.IsolationLevel;
|
||||
|
||||
|
||||
private static Exception SyncError(string caller) => throw new Exception($"Executed synchronous IDbTransaction function {caller}!");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user