refactor: add SqlKata for SQL generation, move connection handling into ModelRepository
This commit is contained in:
@@ -4,17 +4,30 @@ using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
|
||||
using SqlKata;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public async Task AddMessage(IPKConnection conn, PKMessage msg)
|
||||
public Task AddMessage(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);
|
||||
var query = new Query("messages").AsInsert(new
|
||||
{
|
||||
mid = msg.Mid,
|
||||
guild = msg.Guild,
|
||||
channel = msg.Channel,
|
||||
member = msg.Member,
|
||||
sender = msg.Sender,
|
||||
original_mid = msg.OriginalMid,
|
||||
});
|
||||
_logger.Debug("Stored message {@StoredMessage} in channel {Channel}", msg, msg.Channel);
|
||||
|
||||
// "on conflict do nothing" in the (pretty rare) case of duplicate events coming in from Discord, which would lead to a DB error before
|
||||
return _db.ExecuteQuery(query, extraSql: "on conflict do nothing");
|
||||
}
|
||||
|
||||
// todo: add a Mapper to QuerySingle and move this to SqlKata
|
||||
public async Task<FullMessage?> GetMessage(IPKConnection conn, ulong id)
|
||||
{
|
||||
FullMessage Mapper(PKMessage msg, PKMember member, PKSystem system) =>
|
||||
@@ -26,34 +39,36 @@ namespace PluralKit.Core
|
||||
return result.FirstOrDefault();
|
||||
}
|
||||
|
||||
public async Task DeleteMessage(IPKConnection conn, ulong id)
|
||||
public async Task DeleteMessage(ulong id)
|
||||
{
|
||||
var rowCount = await conn.ExecuteAsync("delete from messages where mid = @Id", new { Id = id });
|
||||
var query = new Query("messages").AsDelete().Where("mid", id);
|
||||
var rowCount = await _db.ExecuteQuery(query);
|
||||
if (rowCount > 0)
|
||||
_logger.Information("Deleted message {MessageId} from database", id);
|
||||
}
|
||||
|
||||
public async Task DeleteMessagesBulk(IPKConnection conn, IReadOnlyCollection<ulong> ids)
|
||||
public async Task DeleteMessagesBulk(IReadOnlyCollection<ulong> ids)
|
||||
{
|
||||
// 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() });
|
||||
var query = new Query("messages").AsDelete().WhereIn("mid", ids.Select(id => (long)id).ToArray());
|
||||
var rowCount = await _db.ExecuteQuery(query);
|
||||
if (rowCount > 0)
|
||||
_logger.Information("Bulk deleted messages ({FoundCount} found) from database: {MessageIds}", rowCount,
|
||||
ids);
|
||||
}
|
||||
|
||||
public async Task<PKMessage?> GetLastMessage(IPKConnection conn, ulong guildId, ulong channelId, ulong accountId)
|
||||
public Task<PKMessage?> GetLastMessage(ulong guildId, ulong channelId, ulong accountId)
|
||||
{
|
||||
// Want to index scan on the (guild, sender, mid) index so need the additional constraint
|
||||
return await conn.QuerySingleOrDefaultAsync<PKMessage>(
|
||||
"select * from messages where guild = @Guild and channel = @Channel and sender = @Sender order by mid desc limit 1", new
|
||||
{
|
||||
Guild = guildId,
|
||||
Channel = channelId,
|
||||
Sender = accountId
|
||||
});
|
||||
var query = new Query("messages")
|
||||
.Where("guild", guildId)
|
||||
.Where("channel", channelId)
|
||||
.Where("sender", accountId)
|
||||
.OrderByDesc("mid")
|
||||
.Limit(1);
|
||||
|
||||
return _db.QueryFirst<PKMessage?>(query);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user