feat: use sqlkata for a few more queries

This commit is contained in:
spiral 2022-12-03 12:04:12 +00:00
parent 8a727c6e80
commit b2e4ff38e3
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
5 changed files with 8 additions and 15 deletions

View File

@ -56,10 +56,10 @@ internal partial class Database: IDatabase
return await conn.ExecuteAsync(query.Sql + $" {extraSql}", query.NamedBindings); return await conn.ExecuteAsync(query.Sql + $" {extraSql}", query.NamedBindings);
} }
public async Task<T> QueryFirst<T>(Query q, string extraSql = "", [CallerMemberName] string queryName = "") public async Task<T> QueryFirst<T>(Query q, string extraSql = "", [CallerMemberName] string queryName = "", bool messages = false)
{ {
var query = _compiler.Compile(q); 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))) using (_metrics.Measure.Timer.Time(CoreMetrics.DatabaseQuery, new MetricTags("Query", queryName)))
return await conn.QueryFirstOrDefaultAsync<T>(query.Sql + $" {extraSql}", query.NamedBindings); return await conn.QueryFirstOrDefaultAsync<T>(query.Sql + $" {extraSql}", query.NamedBindings);
} }

View File

@ -17,7 +17,7 @@ public interface IDatabase
[CallerMemberName] string queryName = ""); [CallerMemberName] string queryName = "");
Task<T> QueryFirst<T>(string q, object param = null, [CallerMemberName] string queryName = "", bool messages = false); Task<T> QueryFirst<T>(string q, object param = null, [CallerMemberName] string queryName = "", bool messages = false);
Task<T> QueryFirst<T>(Query q, string extraSql = "", [CallerMemberName] string queryName = ""); Task<T> QueryFirst<T>(Query q, string extraSql = "", [CallerMemberName] string queryName = "", bool messages = false);
Task<T> QueryFirst<T>(IPKConnection? conn, Query q, string extraSql = "", Task<T> QueryFirst<T>(IPKConnection? conn, Query q, string extraSql = "",
[CallerMemberName] string queryName = ""); [CallerMemberName] string queryName = "");

View File

@ -7,7 +7,7 @@ namespace PluralKit.Core;
public partial class ModelRepository public partial class ModelRepository
{ {
public async Task<ulong?> GetDmChannel(ulong id) public async Task<ulong?> GetDmChannel(ulong id)
=> await _db.Execute(c => c.QueryFirstOrDefaultAsync<ulong?>("select dm_channel from accounts where uid = @id", new { id = id })); => await _db.QueryFirst<ulong?>(new Query("accounts").Select("dm_channel").Where("uid", id));
public async Task<bool> GetAutoproxyEnabled(ulong id) public async Task<bool> GetAutoproxyEnabled(ulong id)
=> await _db.QueryFirst<bool>(new Query("accounts").Select("allow_autoproxy").Where("uid", id)); => await _db.QueryFirst<bool>(new Query("accounts").Select("allow_autoproxy").Where("uid", id));

View File

@ -23,14 +23,8 @@ public partial class ModelRepository
return _db.ExecuteQuery(query, "on conflict do nothing", messages: true); return _db.ExecuteQuery(query, "on conflict do nothing", messages: true);
} }
public async Task<PKMessage?> GetMessage(ulong id) public Task<PKMessage?> GetMessage(ulong id)
{ => _db.QueryFirst<PKMessage?>(new Query("messages").Where("mid", id), messages: true);
return await _db.QueryFirst<PKMessage?>(
"select * from messages where mid = @Id",
new { Id = id },
messages: true
);
}
public async Task<FullMessage?> GetFullMessage(ulong id) public async Task<FullMessage?> GetFullMessage(ulong id)
{ {

View File

@ -1,11 +1,10 @@
using Dapper; using SqlKata;
namespace PluralKit.Core; namespace PluralKit.Core;
public partial class ModelRepository public partial class ModelRepository
{ {
public Task<Counts> GetStats() public Task<Counts> GetStats() => _db.QueryFirst<Counts>(new Query("info"));
=> _db.Execute(conn => conn.QuerySingleAsync<Counts>("select * from info"));
public class Counts public class Counts
{ {