diff --git a/PluralKit.Core/Database/DatabaseQueries.cs b/PluralKit.Core/Database/DatabaseQueries.cs index 073c99dd..25d7597e 100644 --- a/PluralKit.Core/Database/DatabaseQueries.cs +++ b/PluralKit.Core/Database/DatabaseQueries.cs @@ -56,10 +56,10 @@ internal partial class Database: IDatabase return await conn.ExecuteAsync(query.Sql + $" {extraSql}", query.NamedBindings); } - public async Task QueryFirst(Query q, string extraSql = "", [CallerMemberName] string queryName = "") + public async Task QueryFirst(Query q, string extraSql = "", [CallerMemberName] string queryName = "", bool messages = false) { var query = _compiler.Compile(q); - using var conn = await Obtain(); + using var conn = await Obtain(messages); using (_metrics.Measure.Timer.Time(CoreMetrics.DatabaseQuery, new MetricTags("Query", queryName))) return await conn.QueryFirstOrDefaultAsync(query.Sql + $" {extraSql}", query.NamedBindings); } diff --git a/PluralKit.Core/Database/IDatabase.cs b/PluralKit.Core/Database/IDatabase.cs index 8de9870f..6662ede9 100644 --- a/PluralKit.Core/Database/IDatabase.cs +++ b/PluralKit.Core/Database/IDatabase.cs @@ -17,7 +17,7 @@ public interface IDatabase [CallerMemberName] string queryName = ""); Task QueryFirst(string q, object param = null, [CallerMemberName] string queryName = "", bool messages = false); - Task QueryFirst(Query q, string extraSql = "", [CallerMemberName] string queryName = ""); + Task QueryFirst(Query q, string extraSql = "", [CallerMemberName] string queryName = "", bool messages = false); Task QueryFirst(IPKConnection? conn, Query q, string extraSql = "", [CallerMemberName] string queryName = ""); diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Account.cs b/PluralKit.Core/Database/Repository/ModelRepository.Account.cs index 241a4406..a748c579 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Account.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Account.cs @@ -7,7 +7,7 @@ namespace PluralKit.Core; public partial class ModelRepository { public async Task GetDmChannel(ulong id) - => await _db.Execute(c => c.QueryFirstOrDefaultAsync("select dm_channel from accounts where uid = @id", new { id = id })); + => await _db.QueryFirst(new Query("accounts").Select("dm_channel").Where("uid", id)); public async Task GetAutoproxyEnabled(ulong id) => await _db.QueryFirst(new Query("accounts").Select("allow_autoproxy").Where("uid", id)); diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Message.cs b/PluralKit.Core/Database/Repository/ModelRepository.Message.cs index c1a77609..706e3a13 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Message.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Message.cs @@ -23,14 +23,8 @@ public partial class ModelRepository return _db.ExecuteQuery(query, "on conflict do nothing", messages: true); } - public async Task GetMessage(ulong id) - { - return await _db.QueryFirst( - "select * from messages where mid = @Id", - new { Id = id }, - messages: true - ); - } + public Task GetMessage(ulong id) + => _db.QueryFirst(new Query("messages").Where("mid", id), messages: true); public async Task GetFullMessage(ulong id) { diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Stats.cs b/PluralKit.Core/Database/Repository/ModelRepository.Stats.cs index 6eaae8e4..a5a28a1d 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Stats.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Stats.cs @@ -1,11 +1,10 @@ -using Dapper; +using SqlKata; namespace PluralKit.Core; public partial class ModelRepository { - public Task GetStats() - => _db.Execute(conn => conn.QuerySingleAsync("select * from info")); + public Task GetStats() => _db.QueryFirst(new Query("info")); public class Counts {